macvtap.c 19 KB

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