macvtap.c 19 KB

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