macvtap.c 21 KB

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