macvtap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. #include <linux/etherdevice.h>
  2. #include <linux/if_macvlan.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/nsproxy.h>
  5. #include <linux/compat.h>
  6. #include <linux/if_tun.h>
  7. #include <linux/module.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/cache.h>
  10. #include <linux/sched.h>
  11. #include <linux/types.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/wait.h>
  15. #include <linux/cdev.h>
  16. #include <linux/fs.h>
  17. #include <net/net_namespace.h>
  18. #include <net/rtnetlink.h>
  19. #include <net/sock.h>
  20. #include <linux/virtio_net.h>
  21. /*
  22. * A macvtap queue is the central object of this driver, it connects
  23. * an open character device to a macvlan interface. There can be
  24. * multiple queues on one interface, which map back to queues
  25. * implemented in hardware on the underlying device.
  26. *
  27. * macvtap_proto is used to allocate queues through the sock allocation
  28. * mechanism.
  29. *
  30. * TODO: multiqueue support is currently not implemented, even though
  31. * macvtap is basically prepared for that. We will need to add this
  32. * here as well as in virtio-net and qemu to get line rate on 10gbit
  33. * adapters from a guest.
  34. */
  35. struct macvtap_queue {
  36. struct sock sk;
  37. struct socket sock;
  38. struct socket_wq wq;
  39. int vnet_hdr_sz;
  40. struct macvlan_dev *vlan;
  41. struct file *file;
  42. unsigned int flags;
  43. };
  44. static struct proto macvtap_proto = {
  45. .name = "macvtap",
  46. .owner = THIS_MODULE,
  47. .obj_size = sizeof (struct macvtap_queue),
  48. };
  49. /*
  50. * Minor number matches netdev->ifindex, so need a potentially
  51. * large value. This also makes it possible to split the
  52. * tap functionality out again in the future by offering it
  53. * from other drivers besides macvtap. As long as every device
  54. * only has one tap, the interface numbers assure that the
  55. * device nodes are unique.
  56. */
  57. static dev_t macvtap_major;
  58. #define MACVTAP_NUM_DEVS 65536
  59. static struct class *macvtap_class;
  60. static struct cdev macvtap_cdev;
  61. static const struct proto_ops macvtap_socket_ops;
  62. /*
  63. * RCU usage:
  64. * The macvtap_queue and the macvlan_dev are loosely coupled, the
  65. * pointers from one to the other can only be read while rcu_read_lock
  66. * or macvtap_lock is held.
  67. *
  68. * Both the file and the macvlan_dev hold a reference on the macvtap_queue
  69. * through sock_hold(&q->sk). When the macvlan_dev goes away first,
  70. * q->vlan becomes inaccessible. When the files gets closed,
  71. * macvtap_get_queue() fails.
  72. *
  73. * There may still be references to the struct sock inside of the
  74. * queue from outbound SKBs, but these never reference back to the
  75. * file or the dev. The data structure is freed through __sk_free
  76. * when both our references and any pending SKBs are gone.
  77. */
  78. static DEFINE_SPINLOCK(macvtap_lock);
  79. /*
  80. * get_slot: return a [unused/occupied] slot in vlan->taps[]:
  81. * - if 'q' is NULL, return the first empty slot;
  82. * - otherwise, return the slot this pointer occupies.
  83. */
  84. static int get_slot(struct macvlan_dev *vlan, struct macvtap_queue *q)
  85. {
  86. int i;
  87. for (i = 0; i < MAX_MACVTAP_QUEUES; i++) {
  88. if (rcu_dereference(vlan->taps[i]) == q)
  89. return i;
  90. }
  91. /* Should never happen */
  92. BUG_ON(1);
  93. }
  94. static int macvtap_set_queue(struct net_device *dev, struct file *file,
  95. struct macvtap_queue *q)
  96. {
  97. struct macvlan_dev *vlan = netdev_priv(dev);
  98. int index;
  99. int err = -EBUSY;
  100. spin_lock(&macvtap_lock);
  101. if (vlan->numvtaps == MAX_MACVTAP_QUEUES)
  102. goto out;
  103. err = 0;
  104. index = get_slot(vlan, NULL);
  105. rcu_assign_pointer(q->vlan, vlan);
  106. rcu_assign_pointer(vlan->taps[index], q);
  107. sock_hold(&q->sk);
  108. q->file = file;
  109. file->private_data = q;
  110. vlan->numvtaps++;
  111. out:
  112. spin_unlock(&macvtap_lock);
  113. return err;
  114. }
  115. /*
  116. * The file owning the queue got closed, give up both
  117. * the reference that the files holds as well as the
  118. * one from the macvlan_dev if that still exists.
  119. *
  120. * Using the spinlock makes sure that we don't get
  121. * to the queue again after destroying it.
  122. */
  123. static void macvtap_put_queue(struct macvtap_queue *q)
  124. {
  125. struct macvlan_dev *vlan;
  126. spin_lock(&macvtap_lock);
  127. vlan = rcu_dereference(q->vlan);
  128. if (vlan) {
  129. int index = get_slot(vlan, q);
  130. rcu_assign_pointer(vlan->taps[index], NULL);
  131. rcu_assign_pointer(q->vlan, NULL);
  132. sock_put(&q->sk);
  133. --vlan->numvtaps;
  134. }
  135. spin_unlock(&macvtap_lock);
  136. synchronize_rcu();
  137. sock_put(&q->sk);
  138. }
  139. /*
  140. * Select a queue based on the rxq of the device on which this packet
  141. * arrived. If the incoming device is not mq, calculate a flow hash
  142. * to select a queue. If all fails, find the first available queue.
  143. * Cache vlan->numvtaps since it can become zero during the execution
  144. * of this function.
  145. */
  146. static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
  147. struct sk_buff *skb)
  148. {
  149. struct macvlan_dev *vlan = netdev_priv(dev);
  150. struct macvtap_queue *tap = NULL;
  151. int numvtaps = vlan->numvtaps;
  152. __u32 rxq;
  153. if (!numvtaps)
  154. goto out;
  155. if (likely(skb_rx_queue_recorded(skb))) {
  156. rxq = skb_get_rx_queue(skb);
  157. while (unlikely(rxq >= numvtaps))
  158. rxq -= numvtaps;
  159. tap = rcu_dereference(vlan->taps[rxq]);
  160. if (tap)
  161. goto out;
  162. }
  163. /* Check if we can use flow to select a queue */
  164. rxq = skb_get_rxhash(skb);
  165. if (rxq) {
  166. tap = rcu_dereference(vlan->taps[rxq % numvtaps]);
  167. if (tap)
  168. goto out;
  169. }
  170. /* Everything failed - find first available queue */
  171. for (rxq = 0; rxq < MAX_MACVTAP_QUEUES; rxq++) {
  172. tap = rcu_dereference(vlan->taps[rxq]);
  173. if (tap)
  174. break;
  175. }
  176. out:
  177. return tap;
  178. }
  179. /*
  180. * The net_device is going away, give up the reference
  181. * that it holds on all queues and safely set the pointer
  182. * from the queues to NULL.
  183. */
  184. static void macvtap_del_queues(struct net_device *dev)
  185. {
  186. struct macvlan_dev *vlan = netdev_priv(dev);
  187. struct macvtap_queue *q, *qlist[MAX_MACVTAP_QUEUES];
  188. int i, j = 0;
  189. /* macvtap_put_queue can free some slots, so go through all slots */
  190. spin_lock(&macvtap_lock);
  191. for (i = 0; i < MAX_MACVTAP_QUEUES && vlan->numvtaps; i++) {
  192. q = rcu_dereference(vlan->taps[i]);
  193. if (q) {
  194. qlist[j++] = q;
  195. rcu_assign_pointer(vlan->taps[i], NULL);
  196. rcu_assign_pointer(q->vlan, NULL);
  197. vlan->numvtaps--;
  198. }
  199. }
  200. BUG_ON(vlan->numvtaps != 0);
  201. spin_unlock(&macvtap_lock);
  202. synchronize_rcu();
  203. for (--j; j >= 0; j--)
  204. sock_put(&qlist[j]->sk);
  205. }
  206. /*
  207. * Forward happens for data that gets sent from one macvlan
  208. * endpoint to another one in bridge mode. We just take
  209. * the skb and put it into the receive queue.
  210. */
  211. static int macvtap_forward(struct net_device *dev, struct sk_buff *skb)
  212. {
  213. struct macvtap_queue *q = macvtap_get_queue(dev, skb);
  214. if (!q)
  215. goto drop;
  216. if (skb_queue_len(&q->sk.sk_receive_queue) >= dev->tx_queue_len)
  217. goto drop;
  218. skb_queue_tail(&q->sk.sk_receive_queue, skb);
  219. wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND);
  220. return NET_RX_SUCCESS;
  221. drop:
  222. kfree_skb(skb);
  223. return NET_RX_DROP;
  224. }
  225. /*
  226. * Receive is for data from the external interface (lowerdev),
  227. * in case of macvtap, we can treat that the same way as
  228. * forward, which macvlan cannot.
  229. */
  230. static int macvtap_receive(struct sk_buff *skb)
  231. {
  232. skb_push(skb, ETH_HLEN);
  233. return macvtap_forward(skb->dev, skb);
  234. }
  235. static int macvtap_newlink(struct net *src_net,
  236. struct net_device *dev,
  237. struct nlattr *tb[],
  238. struct nlattr *data[])
  239. {
  240. struct device *classdev;
  241. dev_t devt;
  242. int err;
  243. err = macvlan_common_newlink(src_net, dev, tb, data,
  244. macvtap_receive, macvtap_forward);
  245. if (err)
  246. goto out;
  247. devt = MKDEV(MAJOR(macvtap_major), dev->ifindex);
  248. classdev = device_create(macvtap_class, &dev->dev, devt,
  249. dev, "tap%d", dev->ifindex);
  250. if (IS_ERR(classdev)) {
  251. err = PTR_ERR(classdev);
  252. macvtap_del_queues(dev);
  253. }
  254. out:
  255. return err;
  256. }
  257. static void macvtap_dellink(struct net_device *dev,
  258. struct list_head *head)
  259. {
  260. device_destroy(macvtap_class,
  261. MKDEV(MAJOR(macvtap_major), dev->ifindex));
  262. macvtap_del_queues(dev);
  263. macvlan_dellink(dev, head);
  264. }
  265. static void macvtap_setup(struct net_device *dev)
  266. {
  267. macvlan_common_setup(dev);
  268. dev->tx_queue_len = TUN_READQ_SIZE;
  269. }
  270. static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
  271. .kind = "macvtap",
  272. .setup = macvtap_setup,
  273. .newlink = macvtap_newlink,
  274. .dellink = macvtap_dellink,
  275. };
  276. static void macvtap_sock_write_space(struct sock *sk)
  277. {
  278. wait_queue_head_t *wqueue;
  279. if (!sock_writeable(sk) ||
  280. !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  281. return;
  282. wqueue = sk_sleep(sk);
  283. if (wqueue && waitqueue_active(wqueue))
  284. wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
  285. }
  286. static int macvtap_open(struct inode *inode, struct file *file)
  287. {
  288. struct net *net = current->nsproxy->net_ns;
  289. struct net_device *dev = dev_get_by_index(net, iminor(inode));
  290. struct macvtap_queue *q;
  291. int err;
  292. err = -ENODEV;
  293. if (!dev)
  294. goto out;
  295. /* check if this is a macvtap device */
  296. err = -EINVAL;
  297. if (dev->rtnl_link_ops != &macvtap_link_ops)
  298. goto out;
  299. err = -ENOMEM;
  300. q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  301. &macvtap_proto);
  302. if (!q)
  303. goto out;
  304. q->sock.wq = &q->wq;
  305. init_waitqueue_head(&q->wq.wait);
  306. q->sock.type = SOCK_RAW;
  307. q->sock.state = SS_CONNECTED;
  308. q->sock.file = file;
  309. q->sock.ops = &macvtap_socket_ops;
  310. sock_init_data(&q->sock, &q->sk);
  311. q->sk.sk_write_space = macvtap_sock_write_space;
  312. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  313. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  314. err = macvtap_set_queue(dev, file, q);
  315. if (err)
  316. sock_put(&q->sk);
  317. out:
  318. if (dev)
  319. dev_put(dev);
  320. return err;
  321. }
  322. static int macvtap_release(struct inode *inode, struct file *file)
  323. {
  324. struct macvtap_queue *q = file->private_data;
  325. macvtap_put_queue(q);
  326. return 0;
  327. }
  328. static unsigned int macvtap_poll(struct file *file, poll_table * wait)
  329. {
  330. struct macvtap_queue *q = file->private_data;
  331. unsigned int mask = POLLERR;
  332. if (!q)
  333. goto out;
  334. mask = 0;
  335. poll_wait(file, &q->wq.wait, wait);
  336. if (!skb_queue_empty(&q->sk.sk_receive_queue))
  337. mask |= POLLIN | POLLRDNORM;
  338. if (sock_writeable(&q->sk) ||
  339. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock.flags) &&
  340. sock_writeable(&q->sk)))
  341. mask |= POLLOUT | POLLWRNORM;
  342. out:
  343. return mask;
  344. }
  345. static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
  346. size_t len, size_t linear,
  347. int noblock, int *err)
  348. {
  349. struct sk_buff *skb;
  350. /* Under a page? Don't bother with paged skb. */
  351. if (prepad + len < PAGE_SIZE || !linear)
  352. linear = len;
  353. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  354. err);
  355. if (!skb)
  356. return NULL;
  357. skb_reserve(skb, prepad);
  358. skb_put(skb, linear);
  359. skb->data_len = len - linear;
  360. skb->len += len - linear;
  361. return skb;
  362. }
  363. /*
  364. * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  365. * be shared with the tun/tap driver.
  366. */
  367. static int macvtap_skb_from_vnet_hdr(struct sk_buff *skb,
  368. struct virtio_net_hdr *vnet_hdr)
  369. {
  370. unsigned short gso_type = 0;
  371. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  372. switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  373. case VIRTIO_NET_HDR_GSO_TCPV4:
  374. gso_type = SKB_GSO_TCPV4;
  375. break;
  376. case VIRTIO_NET_HDR_GSO_TCPV6:
  377. gso_type = SKB_GSO_TCPV6;
  378. break;
  379. case VIRTIO_NET_HDR_GSO_UDP:
  380. gso_type = SKB_GSO_UDP;
  381. break;
  382. default:
  383. return -EINVAL;
  384. }
  385. if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
  386. gso_type |= SKB_GSO_TCP_ECN;
  387. if (vnet_hdr->gso_size == 0)
  388. return -EINVAL;
  389. }
  390. if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  391. if (!skb_partial_csum_set(skb, vnet_hdr->csum_start,
  392. vnet_hdr->csum_offset))
  393. return -EINVAL;
  394. }
  395. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  396. skb_shinfo(skb)->gso_size = vnet_hdr->gso_size;
  397. skb_shinfo(skb)->gso_type = gso_type;
  398. /* Header must be checked, and gso_segs computed. */
  399. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  400. skb_shinfo(skb)->gso_segs = 0;
  401. }
  402. return 0;
  403. }
  404. static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
  405. struct virtio_net_hdr *vnet_hdr)
  406. {
  407. memset(vnet_hdr, 0, sizeof(*vnet_hdr));
  408. if (skb_is_gso(skb)) {
  409. struct skb_shared_info *sinfo = skb_shinfo(skb);
  410. /* This is a hint as to how much should be linear. */
  411. vnet_hdr->hdr_len = skb_headlen(skb);
  412. vnet_hdr->gso_size = sinfo->gso_size;
  413. if (sinfo->gso_type & SKB_GSO_TCPV4)
  414. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  415. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  416. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  417. else if (sinfo->gso_type & SKB_GSO_UDP)
  418. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  419. else
  420. BUG();
  421. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  422. vnet_hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  423. } else
  424. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  425. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  426. vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  427. vnet_hdr->csum_start = skb_checksum_start_offset(skb);
  428. vnet_hdr->csum_offset = skb->csum_offset;
  429. } /* else everything is zero */
  430. return 0;
  431. }
  432. /* Get packet from user space buffer */
  433. static ssize_t macvtap_get_user(struct macvtap_queue *q,
  434. const struct iovec *iv, size_t count,
  435. int noblock)
  436. {
  437. struct sk_buff *skb;
  438. struct macvlan_dev *vlan;
  439. size_t len = count;
  440. int err;
  441. struct virtio_net_hdr vnet_hdr = { 0 };
  442. int vnet_hdr_len = 0;
  443. if (q->flags & IFF_VNET_HDR) {
  444. vnet_hdr_len = q->vnet_hdr_sz;
  445. err = -EINVAL;
  446. if ((len -= vnet_hdr_len) < 0)
  447. goto err;
  448. err = memcpy_fromiovecend((void *)&vnet_hdr, iv, 0,
  449. sizeof(vnet_hdr));
  450. if (err < 0)
  451. goto err;
  452. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  453. vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
  454. vnet_hdr.hdr_len)
  455. vnet_hdr.hdr_len = vnet_hdr.csum_start +
  456. vnet_hdr.csum_offset + 2;
  457. err = -EINVAL;
  458. if (vnet_hdr.hdr_len > len)
  459. goto err;
  460. }
  461. err = -EINVAL;
  462. if (unlikely(len < ETH_HLEN))
  463. goto err;
  464. skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, len, vnet_hdr.hdr_len,
  465. noblock, &err);
  466. if (!skb)
  467. goto err;
  468. err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len, len);
  469. if (err)
  470. goto err_kfree;
  471. skb_set_network_header(skb, ETH_HLEN);
  472. skb_reset_mac_header(skb);
  473. skb->protocol = eth_hdr(skb)->h_proto;
  474. if (vnet_hdr_len) {
  475. err = macvtap_skb_from_vnet_hdr(skb, &vnet_hdr);
  476. if (err)
  477. goto err_kfree;
  478. }
  479. rcu_read_lock_bh();
  480. vlan = rcu_dereference(q->vlan);
  481. if (vlan)
  482. macvlan_start_xmit(skb, vlan->dev);
  483. else
  484. kfree_skb(skb);
  485. rcu_read_unlock_bh();
  486. return count;
  487. err_kfree:
  488. kfree_skb(skb);
  489. err:
  490. rcu_read_lock_bh();
  491. vlan = rcu_dereference(q->vlan);
  492. if (vlan)
  493. vlan->dev->stats.tx_dropped++;
  494. rcu_read_unlock_bh();
  495. return err;
  496. }
  497. static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
  498. unsigned long count, loff_t pos)
  499. {
  500. struct file *file = iocb->ki_filp;
  501. ssize_t result = -ENOLINK;
  502. struct macvtap_queue *q = file->private_data;
  503. result = macvtap_get_user(q, iv, iov_length(iv, count),
  504. file->f_flags & O_NONBLOCK);
  505. return result;
  506. }
  507. /* Put packet to the user space buffer */
  508. static ssize_t macvtap_put_user(struct macvtap_queue *q,
  509. const struct sk_buff *skb,
  510. const struct iovec *iv, int len)
  511. {
  512. struct macvlan_dev *vlan;
  513. int ret;
  514. int vnet_hdr_len = 0;
  515. if (q->flags & IFF_VNET_HDR) {
  516. struct virtio_net_hdr vnet_hdr;
  517. vnet_hdr_len = q->vnet_hdr_sz;
  518. if ((len -= vnet_hdr_len) < 0)
  519. return -EINVAL;
  520. ret = macvtap_skb_to_vnet_hdr(skb, &vnet_hdr);
  521. if (ret)
  522. return ret;
  523. if (memcpy_toiovecend(iv, (void *)&vnet_hdr, 0, sizeof(vnet_hdr)))
  524. return -EFAULT;
  525. }
  526. len = min_t(int, skb->len, len);
  527. ret = skb_copy_datagram_const_iovec(skb, 0, iv, vnet_hdr_len, len);
  528. rcu_read_lock_bh();
  529. vlan = rcu_dereference(q->vlan);
  530. if (vlan)
  531. macvlan_count_rx(vlan, len, ret == 0, 0);
  532. rcu_read_unlock_bh();
  533. return ret ? ret : (len + vnet_hdr_len);
  534. }
  535. static ssize_t macvtap_do_read(struct macvtap_queue *q, struct kiocb *iocb,
  536. const struct iovec *iv, unsigned long len,
  537. int noblock)
  538. {
  539. DECLARE_WAITQUEUE(wait, current);
  540. struct sk_buff *skb;
  541. ssize_t ret = 0;
  542. add_wait_queue(sk_sleep(&q->sk), &wait);
  543. while (len) {
  544. current->state = TASK_INTERRUPTIBLE;
  545. /* Read frames from the queue */
  546. skb = skb_dequeue(&q->sk.sk_receive_queue);
  547. if (!skb) {
  548. if (noblock) {
  549. ret = -EAGAIN;
  550. break;
  551. }
  552. if (signal_pending(current)) {
  553. ret = -ERESTARTSYS;
  554. break;
  555. }
  556. /* Nothing to read, let's sleep */
  557. schedule();
  558. continue;
  559. }
  560. ret = macvtap_put_user(q, skb, iv, len);
  561. kfree_skb(skb);
  562. break;
  563. }
  564. current->state = TASK_RUNNING;
  565. remove_wait_queue(sk_sleep(&q->sk), &wait);
  566. return ret;
  567. }
  568. static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
  569. unsigned long count, loff_t pos)
  570. {
  571. struct file *file = iocb->ki_filp;
  572. struct macvtap_queue *q = file->private_data;
  573. ssize_t len, ret = 0;
  574. len = iov_length(iv, count);
  575. if (len < 0) {
  576. ret = -EINVAL;
  577. goto out;
  578. }
  579. ret = macvtap_do_read(q, iocb, iv, len, file->f_flags & O_NONBLOCK);
  580. ret = min_t(ssize_t, ret, len); /* XXX copied from tun.c. Why? */
  581. out:
  582. return ret;
  583. }
  584. /*
  585. * provide compatibility with generic tun/tap interface
  586. */
  587. static long macvtap_ioctl(struct file *file, unsigned int cmd,
  588. unsigned long arg)
  589. {
  590. struct macvtap_queue *q = file->private_data;
  591. struct macvlan_dev *vlan;
  592. void __user *argp = (void __user *)arg;
  593. struct ifreq __user *ifr = argp;
  594. unsigned int __user *up = argp;
  595. unsigned int u;
  596. int __user *sp = argp;
  597. int s;
  598. int ret;
  599. switch (cmd) {
  600. case TUNSETIFF:
  601. /* ignore the name, just look at flags */
  602. if (get_user(u, &ifr->ifr_flags))
  603. return -EFAULT;
  604. ret = 0;
  605. if ((u & ~IFF_VNET_HDR) != (IFF_NO_PI | IFF_TAP))
  606. ret = -EINVAL;
  607. else
  608. q->flags = u;
  609. return ret;
  610. case TUNGETIFF:
  611. rcu_read_lock_bh();
  612. vlan = rcu_dereference(q->vlan);
  613. if (vlan)
  614. dev_hold(vlan->dev);
  615. rcu_read_unlock_bh();
  616. if (!vlan)
  617. return -ENOLINK;
  618. ret = 0;
  619. if (copy_to_user(&ifr->ifr_name, q->vlan->dev->name, IFNAMSIZ) ||
  620. put_user(q->flags, &ifr->ifr_flags))
  621. ret = -EFAULT;
  622. dev_put(vlan->dev);
  623. return ret;
  624. case TUNGETFEATURES:
  625. if (put_user(IFF_TAP | IFF_NO_PI | IFF_VNET_HDR, up))
  626. return -EFAULT;
  627. return 0;
  628. case TUNSETSNDBUF:
  629. if (get_user(u, up))
  630. return -EFAULT;
  631. q->sk.sk_sndbuf = u;
  632. return 0;
  633. case TUNGETVNETHDRSZ:
  634. s = q->vnet_hdr_sz;
  635. if (put_user(s, sp))
  636. return -EFAULT;
  637. return 0;
  638. case TUNSETVNETHDRSZ:
  639. if (get_user(s, sp))
  640. return -EFAULT;
  641. if (s < (int)sizeof(struct virtio_net_hdr))
  642. return -EINVAL;
  643. q->vnet_hdr_sz = s;
  644. return 0;
  645. case TUNSETOFFLOAD:
  646. /* let the user check for future flags */
  647. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  648. TUN_F_TSO_ECN | TUN_F_UFO))
  649. return -EINVAL;
  650. /* TODO: only accept frames with the features that
  651. got enabled for forwarded frames */
  652. if (!(q->flags & IFF_VNET_HDR))
  653. return -EINVAL;
  654. return 0;
  655. default:
  656. return -EINVAL;
  657. }
  658. }
  659. #ifdef CONFIG_COMPAT
  660. static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
  661. unsigned long arg)
  662. {
  663. return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  664. }
  665. #endif
  666. static const struct file_operations macvtap_fops = {
  667. .owner = THIS_MODULE,
  668. .open = macvtap_open,
  669. .release = macvtap_release,
  670. .aio_read = macvtap_aio_read,
  671. .aio_write = macvtap_aio_write,
  672. .poll = macvtap_poll,
  673. .llseek = no_llseek,
  674. .unlocked_ioctl = macvtap_ioctl,
  675. #ifdef CONFIG_COMPAT
  676. .compat_ioctl = macvtap_compat_ioctl,
  677. #endif
  678. };
  679. static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
  680. struct msghdr *m, size_t total_len)
  681. {
  682. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  683. return macvtap_get_user(q, m->msg_iov, total_len,
  684. m->msg_flags & MSG_DONTWAIT);
  685. }
  686. static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
  687. struct msghdr *m, size_t total_len,
  688. int flags)
  689. {
  690. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  691. int ret;
  692. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  693. return -EINVAL;
  694. ret = macvtap_do_read(q, iocb, m->msg_iov, total_len,
  695. flags & MSG_DONTWAIT);
  696. if (ret > total_len) {
  697. m->msg_flags |= MSG_TRUNC;
  698. ret = flags & MSG_TRUNC ? ret : total_len;
  699. }
  700. return ret;
  701. }
  702. /* Ops structure to mimic raw sockets with tun */
  703. static const struct proto_ops macvtap_socket_ops = {
  704. .sendmsg = macvtap_sendmsg,
  705. .recvmsg = macvtap_recvmsg,
  706. };
  707. /* Get an underlying socket object from tun file. Returns error unless file is
  708. * attached to a device. The returned object works like a packet socket, it
  709. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  710. * holding a reference to the file for as long as the socket is in use. */
  711. struct socket *macvtap_get_socket(struct file *file)
  712. {
  713. struct macvtap_queue *q;
  714. if (file->f_op != &macvtap_fops)
  715. return ERR_PTR(-EINVAL);
  716. q = file->private_data;
  717. if (!q)
  718. return ERR_PTR(-EBADFD);
  719. return &q->sock;
  720. }
  721. EXPORT_SYMBOL_GPL(macvtap_get_socket);
  722. static int macvtap_init(void)
  723. {
  724. int err;
  725. err = alloc_chrdev_region(&macvtap_major, 0,
  726. MACVTAP_NUM_DEVS, "macvtap");
  727. if (err)
  728. goto out1;
  729. cdev_init(&macvtap_cdev, &macvtap_fops);
  730. err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
  731. if (err)
  732. goto out2;
  733. macvtap_class = class_create(THIS_MODULE, "macvtap");
  734. if (IS_ERR(macvtap_class)) {
  735. err = PTR_ERR(macvtap_class);
  736. goto out3;
  737. }
  738. err = macvlan_link_register(&macvtap_link_ops);
  739. if (err)
  740. goto out4;
  741. return 0;
  742. out4:
  743. class_unregister(macvtap_class);
  744. out3:
  745. cdev_del(&macvtap_cdev);
  746. out2:
  747. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  748. out1:
  749. return err;
  750. }
  751. module_init(macvtap_init);
  752. static void macvtap_exit(void)
  753. {
  754. rtnl_link_unregister(&macvtap_link_ops);
  755. class_unregister(macvtap_class);
  756. cdev_del(&macvtap_cdev);
  757. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  758. }
  759. module_exit(macvtap_exit);
  760. MODULE_ALIAS_RTNL_LINK("macvtap");
  761. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  762. MODULE_LICENSE("GPL");