macvtap.c 19 KB

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