macvtap.c 20 KB

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