macvtap.c 24 KB

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