macvtap.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. #include <linux/etherdevice.h>
  2. #include <linux/if_macvlan.h>
  3. #include <linux/if_vlan.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/nsproxy.h>
  6. #include <linux/compat.h>
  7. #include <linux/if_tun.h>
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/cache.h>
  11. #include <linux/sched.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/wait.h>
  15. #include <linux/cdev.h>
  16. #include <linux/idr.h>
  17. #include <linux/fs.h>
  18. #include <linux/uio.h>
  19. #include <net/net_namespace.h>
  20. #include <net/rtnetlink.h>
  21. #include <net/sock.h>
  22. #include <linux/virtio_net.h>
  23. /*
  24. * A macvtap queue is the central object of this driver, it connects
  25. * an open character device to a macvlan interface. There can be
  26. * multiple queues on one interface, which map back to queues
  27. * implemented in hardware on the underlying device.
  28. *
  29. * macvtap_proto is used to allocate queues through the sock allocation
  30. * mechanism.
  31. *
  32. */
  33. struct macvtap_queue {
  34. struct sock sk;
  35. struct socket sock;
  36. struct socket_wq wq;
  37. int vnet_hdr_sz;
  38. struct macvlan_dev __rcu *vlan;
  39. struct file *file;
  40. unsigned int flags;
  41. u16 queue_index;
  42. bool enabled;
  43. struct list_head next;
  44. };
  45. #define MACVTAP_FEATURES (IFF_VNET_HDR | IFF_MULTI_QUEUE)
  46. #define MACVTAP_VNET_LE 0x80000000
  47. #define MACVTAP_VNET_BE 0x40000000
  48. #ifdef CONFIG_TUN_VNET_CROSS_LE
  49. static inline bool macvtap_legacy_is_little_endian(struct macvtap_queue *q)
  50. {
  51. return q->flags & MACVTAP_VNET_BE ? false :
  52. virtio_legacy_is_little_endian();
  53. }
  54. static long macvtap_get_vnet_be(struct macvtap_queue *q, int __user *sp)
  55. {
  56. int s = !!(q->flags & MACVTAP_VNET_BE);
  57. if (put_user(s, sp))
  58. return -EFAULT;
  59. return 0;
  60. }
  61. static long macvtap_set_vnet_be(struct macvtap_queue *q, int __user *sp)
  62. {
  63. int s;
  64. if (get_user(s, sp))
  65. return -EFAULT;
  66. if (s)
  67. q->flags |= MACVTAP_VNET_BE;
  68. else
  69. q->flags &= ~MACVTAP_VNET_BE;
  70. return 0;
  71. }
  72. #else
  73. static inline bool macvtap_legacy_is_little_endian(struct macvtap_queue *q)
  74. {
  75. return virtio_legacy_is_little_endian();
  76. }
  77. static long macvtap_get_vnet_be(struct macvtap_queue *q, int __user *argp)
  78. {
  79. return -EINVAL;
  80. }
  81. static long macvtap_set_vnet_be(struct macvtap_queue *q, int __user *argp)
  82. {
  83. return -EINVAL;
  84. }
  85. #endif /* CONFIG_TUN_VNET_CROSS_LE */
  86. static inline bool macvtap_is_little_endian(struct macvtap_queue *q)
  87. {
  88. return q->flags & MACVTAP_VNET_LE ||
  89. macvtap_legacy_is_little_endian(q);
  90. }
  91. static inline u16 macvtap16_to_cpu(struct macvtap_queue *q, __virtio16 val)
  92. {
  93. return __virtio16_to_cpu(macvtap_is_little_endian(q), val);
  94. }
  95. static inline __virtio16 cpu_to_macvtap16(struct macvtap_queue *q, u16 val)
  96. {
  97. return __cpu_to_virtio16(macvtap_is_little_endian(q), val);
  98. }
  99. static struct proto macvtap_proto = {
  100. .name = "macvtap",
  101. .owner = THIS_MODULE,
  102. .obj_size = sizeof (struct macvtap_queue),
  103. };
  104. /*
  105. * Variables for dealing with macvtaps device numbers.
  106. */
  107. static dev_t macvtap_major;
  108. #define MACVTAP_NUM_DEVS (1U << MINORBITS)
  109. static DEFINE_MUTEX(minor_lock);
  110. static DEFINE_IDR(minor_idr);
  111. #define GOODCOPY_LEN 128
  112. static struct class *macvtap_class;
  113. static struct cdev macvtap_cdev;
  114. static const struct proto_ops macvtap_socket_ops;
  115. #define TUN_OFFLOADS (NETIF_F_HW_CSUM | NETIF_F_TSO_ECN | NETIF_F_TSO | \
  116. NETIF_F_TSO6 | NETIF_F_UFO)
  117. #define RX_OFFLOADS (NETIF_F_GRO | NETIF_F_LRO)
  118. #define TAP_FEATURES (NETIF_F_GSO | NETIF_F_SG | NETIF_F_FRAGLIST)
  119. static struct macvlan_dev *macvtap_get_vlan_rcu(const struct net_device *dev)
  120. {
  121. return rcu_dereference(dev->rx_handler_data);
  122. }
  123. /*
  124. * RCU usage:
  125. * The macvtap_queue and the macvlan_dev are loosely coupled, the
  126. * pointers from one to the other can only be read while rcu_read_lock
  127. * or rtnl is held.
  128. *
  129. * Both the file and the macvlan_dev hold a reference on the macvtap_queue
  130. * through sock_hold(&q->sk). When the macvlan_dev goes away first,
  131. * q->vlan becomes inaccessible. When the files gets closed,
  132. * macvtap_get_queue() fails.
  133. *
  134. * There may still be references to the struct sock inside of the
  135. * queue from outbound SKBs, but these never reference back to the
  136. * file or the dev. The data structure is freed through __sk_free
  137. * when both our references and any pending SKBs are gone.
  138. */
  139. static int macvtap_enable_queue(struct net_device *dev, struct file *file,
  140. struct macvtap_queue *q)
  141. {
  142. struct macvlan_dev *vlan = netdev_priv(dev);
  143. int err = -EINVAL;
  144. ASSERT_RTNL();
  145. if (q->enabled)
  146. goto out;
  147. err = 0;
  148. rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
  149. q->queue_index = vlan->numvtaps;
  150. q->enabled = true;
  151. vlan->numvtaps++;
  152. out:
  153. return err;
  154. }
  155. /* Requires RTNL */
  156. static int macvtap_set_queue(struct net_device *dev, struct file *file,
  157. struct macvtap_queue *q)
  158. {
  159. struct macvlan_dev *vlan = netdev_priv(dev);
  160. if (vlan->numqueues == MAX_MACVTAP_QUEUES)
  161. return -EBUSY;
  162. rcu_assign_pointer(q->vlan, vlan);
  163. rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
  164. sock_hold(&q->sk);
  165. q->file = file;
  166. q->queue_index = vlan->numvtaps;
  167. q->enabled = true;
  168. file->private_data = q;
  169. list_add_tail(&q->next, &vlan->queue_list);
  170. vlan->numvtaps++;
  171. vlan->numqueues++;
  172. return 0;
  173. }
  174. static int macvtap_disable_queue(struct macvtap_queue *q)
  175. {
  176. struct macvlan_dev *vlan;
  177. struct macvtap_queue *nq;
  178. ASSERT_RTNL();
  179. if (!q->enabled)
  180. return -EINVAL;
  181. vlan = rtnl_dereference(q->vlan);
  182. if (vlan) {
  183. int index = q->queue_index;
  184. BUG_ON(index >= vlan->numvtaps);
  185. nq = rtnl_dereference(vlan->taps[vlan->numvtaps - 1]);
  186. nq->queue_index = index;
  187. rcu_assign_pointer(vlan->taps[index], nq);
  188. RCU_INIT_POINTER(vlan->taps[vlan->numvtaps - 1], NULL);
  189. q->enabled = false;
  190. vlan->numvtaps--;
  191. }
  192. return 0;
  193. }
  194. /*
  195. * The file owning the queue got closed, give up both
  196. * the reference that the files holds as well as the
  197. * one from the macvlan_dev if that still exists.
  198. *
  199. * Using the spinlock makes sure that we don't get
  200. * to the queue again after destroying it.
  201. */
  202. static void macvtap_put_queue(struct macvtap_queue *q)
  203. {
  204. struct macvlan_dev *vlan;
  205. rtnl_lock();
  206. vlan = rtnl_dereference(q->vlan);
  207. if (vlan) {
  208. if (q->enabled)
  209. BUG_ON(macvtap_disable_queue(q));
  210. vlan->numqueues--;
  211. RCU_INIT_POINTER(q->vlan, NULL);
  212. sock_put(&q->sk);
  213. list_del_init(&q->next);
  214. }
  215. rtnl_unlock();
  216. synchronize_rcu();
  217. sock_put(&q->sk);
  218. }
  219. /*
  220. * Select a queue based on the rxq of the device on which this packet
  221. * arrived. If the incoming device is not mq, calculate a flow hash
  222. * to select a queue. If all fails, find the first available queue.
  223. * Cache vlan->numvtaps since it can become zero during the execution
  224. * of this function.
  225. */
  226. static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
  227. struct sk_buff *skb)
  228. {
  229. struct macvlan_dev *vlan = netdev_priv(dev);
  230. struct macvtap_queue *tap = NULL;
  231. /* Access to taps array is protected by rcu, but access to numvtaps
  232. * isn't. Below we use it to lookup a queue, but treat it as a hint
  233. * and validate that the result isn't NULL - in case we are
  234. * racing against queue removal.
  235. */
  236. int numvtaps = ACCESS_ONCE(vlan->numvtaps);
  237. __u32 rxq;
  238. if (!numvtaps)
  239. goto out;
  240. /* Check if we can use flow to select a queue */
  241. rxq = skb_get_hash(skb);
  242. if (rxq) {
  243. tap = rcu_dereference(vlan->taps[rxq % numvtaps]);
  244. goto out;
  245. }
  246. if (likely(skb_rx_queue_recorded(skb))) {
  247. rxq = skb_get_rx_queue(skb);
  248. while (unlikely(rxq >= numvtaps))
  249. rxq -= numvtaps;
  250. tap = rcu_dereference(vlan->taps[rxq]);
  251. goto out;
  252. }
  253. tap = rcu_dereference(vlan->taps[0]);
  254. out:
  255. return tap;
  256. }
  257. /*
  258. * The net_device is going away, give up the reference
  259. * that it holds on all queues and safely set the pointer
  260. * from the queues to NULL.
  261. */
  262. static void macvtap_del_queues(struct net_device *dev)
  263. {
  264. struct macvlan_dev *vlan = netdev_priv(dev);
  265. struct macvtap_queue *q, *tmp;
  266. ASSERT_RTNL();
  267. list_for_each_entry_safe(q, tmp, &vlan->queue_list, next) {
  268. list_del_init(&q->next);
  269. RCU_INIT_POINTER(q->vlan, NULL);
  270. if (q->enabled)
  271. vlan->numvtaps--;
  272. vlan->numqueues--;
  273. sock_put(&q->sk);
  274. }
  275. BUG_ON(vlan->numvtaps);
  276. BUG_ON(vlan->numqueues);
  277. /* guarantee that any future macvtap_set_queue will fail */
  278. vlan->numvtaps = MAX_MACVTAP_QUEUES;
  279. }
  280. static rx_handler_result_t macvtap_handle_frame(struct sk_buff **pskb)
  281. {
  282. struct sk_buff *skb = *pskb;
  283. struct net_device *dev = skb->dev;
  284. struct macvlan_dev *vlan;
  285. struct macvtap_queue *q;
  286. netdev_features_t features = TAP_FEATURES;
  287. vlan = macvtap_get_vlan_rcu(dev);
  288. if (!vlan)
  289. return RX_HANDLER_PASS;
  290. q = macvtap_get_queue(dev, skb);
  291. if (!q)
  292. return RX_HANDLER_PASS;
  293. if (skb_queue_len(&q->sk.sk_receive_queue) >= dev->tx_queue_len)
  294. goto drop;
  295. skb_push(skb, ETH_HLEN);
  296. /* Apply the forward feature mask so that we perform segmentation
  297. * according to users wishes. This only works if VNET_HDR is
  298. * enabled.
  299. */
  300. if (q->flags & IFF_VNET_HDR)
  301. features |= vlan->tap_features;
  302. if (netif_needs_gso(skb, features)) {
  303. struct sk_buff *segs = __skb_gso_segment(skb, features, false);
  304. if (IS_ERR(segs))
  305. goto drop;
  306. if (!segs) {
  307. skb_queue_tail(&q->sk.sk_receive_queue, skb);
  308. goto wake_up;
  309. }
  310. kfree_skb(skb);
  311. while (segs) {
  312. struct sk_buff *nskb = segs->next;
  313. segs->next = NULL;
  314. skb_queue_tail(&q->sk.sk_receive_queue, segs);
  315. segs = nskb;
  316. }
  317. } else {
  318. /* If we receive a partial checksum and the tap side
  319. * doesn't support checksum offload, compute the checksum.
  320. * Note: it doesn't matter which checksum feature to
  321. * check, we either support them all or none.
  322. */
  323. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  324. !(features & NETIF_F_ALL_CSUM) &&
  325. skb_checksum_help(skb))
  326. goto drop;
  327. skb_queue_tail(&q->sk.sk_receive_queue, skb);
  328. }
  329. wake_up:
  330. wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND);
  331. return RX_HANDLER_CONSUMED;
  332. drop:
  333. /* Count errors/drops only here, thus don't care about args. */
  334. macvlan_count_rx(vlan, 0, 0, 0);
  335. kfree_skb(skb);
  336. return RX_HANDLER_CONSUMED;
  337. }
  338. static int macvtap_get_minor(struct macvlan_dev *vlan)
  339. {
  340. int retval = -ENOMEM;
  341. mutex_lock(&minor_lock);
  342. retval = idr_alloc(&minor_idr, vlan, 1, MACVTAP_NUM_DEVS, GFP_KERNEL);
  343. if (retval >= 0) {
  344. vlan->minor = retval;
  345. } else if (retval == -ENOSPC) {
  346. printk(KERN_ERR "too many macvtap devices\n");
  347. retval = -EINVAL;
  348. }
  349. mutex_unlock(&minor_lock);
  350. return retval < 0 ? retval : 0;
  351. }
  352. static void macvtap_free_minor(struct macvlan_dev *vlan)
  353. {
  354. mutex_lock(&minor_lock);
  355. if (vlan->minor) {
  356. idr_remove(&minor_idr, vlan->minor);
  357. vlan->minor = 0;
  358. }
  359. mutex_unlock(&minor_lock);
  360. }
  361. static struct net_device *dev_get_by_macvtap_minor(int minor)
  362. {
  363. struct net_device *dev = NULL;
  364. struct macvlan_dev *vlan;
  365. mutex_lock(&minor_lock);
  366. vlan = idr_find(&minor_idr, minor);
  367. if (vlan) {
  368. dev = vlan->dev;
  369. dev_hold(dev);
  370. }
  371. mutex_unlock(&minor_lock);
  372. return dev;
  373. }
  374. static int macvtap_newlink(struct net *src_net,
  375. struct net_device *dev,
  376. struct nlattr *tb[],
  377. struct nlattr *data[])
  378. {
  379. struct macvlan_dev *vlan = netdev_priv(dev);
  380. int err;
  381. INIT_LIST_HEAD(&vlan->queue_list);
  382. /* Since macvlan supports all offloads by default, make
  383. * tap support all offloads also.
  384. */
  385. vlan->tap_features = TUN_OFFLOADS;
  386. err = netdev_rx_handler_register(dev, macvtap_handle_frame, vlan);
  387. if (err)
  388. return err;
  389. /* Don't put anything that may fail after macvlan_common_newlink
  390. * because we can't undo what it does.
  391. */
  392. return macvlan_common_newlink(src_net, dev, tb, data);
  393. }
  394. static void macvtap_dellink(struct net_device *dev,
  395. struct list_head *head)
  396. {
  397. netdev_rx_handler_unregister(dev);
  398. macvtap_del_queues(dev);
  399. macvlan_dellink(dev, head);
  400. }
  401. static void macvtap_setup(struct net_device *dev)
  402. {
  403. macvlan_common_setup(dev);
  404. dev->tx_queue_len = TUN_READQ_SIZE;
  405. }
  406. static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
  407. .kind = "macvtap",
  408. .setup = macvtap_setup,
  409. .newlink = macvtap_newlink,
  410. .dellink = macvtap_dellink,
  411. };
  412. static void macvtap_sock_write_space(struct sock *sk)
  413. {
  414. wait_queue_head_t *wqueue;
  415. if (!sock_writeable(sk) ||
  416. !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  417. return;
  418. wqueue = sk_sleep(sk);
  419. if (wqueue && waitqueue_active(wqueue))
  420. wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
  421. }
  422. static void macvtap_sock_destruct(struct sock *sk)
  423. {
  424. skb_queue_purge(&sk->sk_receive_queue);
  425. }
  426. static int macvtap_open(struct inode *inode, struct file *file)
  427. {
  428. struct net *net = current->nsproxy->net_ns;
  429. struct net_device *dev;
  430. struct macvtap_queue *q;
  431. int err = -ENODEV;
  432. rtnl_lock();
  433. dev = dev_get_by_macvtap_minor(iminor(inode));
  434. if (!dev)
  435. goto out;
  436. err = -ENOMEM;
  437. q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  438. &macvtap_proto, 0);
  439. if (!q)
  440. goto out;
  441. RCU_INIT_POINTER(q->sock.wq, &q->wq);
  442. init_waitqueue_head(&q->wq.wait);
  443. q->sock.type = SOCK_RAW;
  444. q->sock.state = SS_CONNECTED;
  445. q->sock.file = file;
  446. q->sock.ops = &macvtap_socket_ops;
  447. sock_init_data(&q->sock, &q->sk);
  448. q->sk.sk_write_space = macvtap_sock_write_space;
  449. q->sk.sk_destruct = macvtap_sock_destruct;
  450. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  451. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  452. /*
  453. * so far only KVM virtio_net uses macvtap, enable zero copy between
  454. * guest kernel and host kernel when lower device supports zerocopy
  455. *
  456. * The macvlan supports zerocopy iff the lower device supports zero
  457. * copy so we don't have to look at the lower device directly.
  458. */
  459. if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG))
  460. sock_set_flag(&q->sk, SOCK_ZEROCOPY);
  461. err = macvtap_set_queue(dev, file, q);
  462. if (err)
  463. sock_put(&q->sk);
  464. out:
  465. if (dev)
  466. dev_put(dev);
  467. rtnl_unlock();
  468. return err;
  469. }
  470. static int macvtap_release(struct inode *inode, struct file *file)
  471. {
  472. struct macvtap_queue *q = file->private_data;
  473. macvtap_put_queue(q);
  474. return 0;
  475. }
  476. static unsigned int macvtap_poll(struct file *file, poll_table * wait)
  477. {
  478. struct macvtap_queue *q = file->private_data;
  479. unsigned int mask = POLLERR;
  480. if (!q)
  481. goto out;
  482. mask = 0;
  483. poll_wait(file, &q->wq.wait, wait);
  484. if (!skb_queue_empty(&q->sk.sk_receive_queue))
  485. mask |= POLLIN | POLLRDNORM;
  486. if (sock_writeable(&q->sk) ||
  487. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock.flags) &&
  488. sock_writeable(&q->sk)))
  489. mask |= POLLOUT | POLLWRNORM;
  490. out:
  491. return mask;
  492. }
  493. static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
  494. size_t len, size_t linear,
  495. int noblock, int *err)
  496. {
  497. struct sk_buff *skb;
  498. /* Under a page? Don't bother with paged skb. */
  499. if (prepad + len < PAGE_SIZE || !linear)
  500. linear = len;
  501. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  502. err, 0);
  503. if (!skb)
  504. return NULL;
  505. skb_reserve(skb, prepad);
  506. skb_put(skb, linear);
  507. skb->data_len = len - linear;
  508. skb->len += len - linear;
  509. return skb;
  510. }
  511. /*
  512. * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  513. * be shared with the tun/tap driver.
  514. */
  515. static int macvtap_skb_from_vnet_hdr(struct macvtap_queue *q,
  516. struct sk_buff *skb,
  517. struct virtio_net_hdr *vnet_hdr)
  518. {
  519. unsigned short gso_type = 0;
  520. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  521. switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  522. case VIRTIO_NET_HDR_GSO_TCPV4:
  523. gso_type = SKB_GSO_TCPV4;
  524. break;
  525. case VIRTIO_NET_HDR_GSO_TCPV6:
  526. gso_type = SKB_GSO_TCPV6;
  527. break;
  528. case VIRTIO_NET_HDR_GSO_UDP:
  529. gso_type = SKB_GSO_UDP;
  530. break;
  531. default:
  532. return -EINVAL;
  533. }
  534. if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
  535. gso_type |= SKB_GSO_TCP_ECN;
  536. if (vnet_hdr->gso_size == 0)
  537. return -EINVAL;
  538. }
  539. if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  540. if (!skb_partial_csum_set(skb, macvtap16_to_cpu(q, vnet_hdr->csum_start),
  541. macvtap16_to_cpu(q, vnet_hdr->csum_offset)))
  542. return -EINVAL;
  543. }
  544. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  545. skb_shinfo(skb)->gso_size = macvtap16_to_cpu(q, vnet_hdr->gso_size);
  546. skb_shinfo(skb)->gso_type = gso_type;
  547. /* Header must be checked, and gso_segs computed. */
  548. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  549. skb_shinfo(skb)->gso_segs = 0;
  550. }
  551. return 0;
  552. }
  553. static void macvtap_skb_to_vnet_hdr(struct macvtap_queue *q,
  554. const struct sk_buff *skb,
  555. struct virtio_net_hdr *vnet_hdr)
  556. {
  557. memset(vnet_hdr, 0, sizeof(*vnet_hdr));
  558. if (skb_is_gso(skb)) {
  559. struct skb_shared_info *sinfo = skb_shinfo(skb);
  560. /* This is a hint as to how much should be linear. */
  561. vnet_hdr->hdr_len = cpu_to_macvtap16(q, skb_headlen(skb));
  562. vnet_hdr->gso_size = cpu_to_macvtap16(q, sinfo->gso_size);
  563. if (sinfo->gso_type & SKB_GSO_TCPV4)
  564. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  565. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  566. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  567. else if (sinfo->gso_type & SKB_GSO_UDP)
  568. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  569. else
  570. BUG();
  571. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  572. vnet_hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  573. } else
  574. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  575. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  576. vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  577. if (skb_vlan_tag_present(skb))
  578. vnet_hdr->csum_start = cpu_to_macvtap16(q,
  579. skb_checksum_start_offset(skb) + VLAN_HLEN);
  580. else
  581. vnet_hdr->csum_start = cpu_to_macvtap16(q,
  582. skb_checksum_start_offset(skb));
  583. vnet_hdr->csum_offset = cpu_to_macvtap16(q, skb->csum_offset);
  584. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  585. vnet_hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
  586. } /* else everything is zero */
  587. }
  588. /* Neighbour code has some assumptions on HH_DATA_MOD alignment */
  589. #define MACVTAP_RESERVE HH_DATA_OFF(ETH_HLEN)
  590. /* Get packet from user space buffer */
  591. static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
  592. struct iov_iter *from, int noblock)
  593. {
  594. int good_linear = SKB_MAX_HEAD(MACVTAP_RESERVE);
  595. struct sk_buff *skb;
  596. struct macvlan_dev *vlan;
  597. unsigned long total_len = iov_iter_count(from);
  598. unsigned long len = total_len;
  599. int err;
  600. struct virtio_net_hdr vnet_hdr = { 0 };
  601. int vnet_hdr_len = 0;
  602. int copylen = 0;
  603. int depth;
  604. bool zerocopy = false;
  605. size_t linear;
  606. ssize_t n;
  607. if (q->flags & IFF_VNET_HDR) {
  608. vnet_hdr_len = q->vnet_hdr_sz;
  609. err = -EINVAL;
  610. if (len < vnet_hdr_len)
  611. goto err;
  612. len -= vnet_hdr_len;
  613. err = -EFAULT;
  614. n = copy_from_iter(&vnet_hdr, sizeof(vnet_hdr), from);
  615. if (n != sizeof(vnet_hdr))
  616. goto err;
  617. iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
  618. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  619. macvtap16_to_cpu(q, vnet_hdr.csum_start) +
  620. macvtap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
  621. macvtap16_to_cpu(q, vnet_hdr.hdr_len))
  622. vnet_hdr.hdr_len = cpu_to_macvtap16(q,
  623. macvtap16_to_cpu(q, vnet_hdr.csum_start) +
  624. macvtap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
  625. err = -EINVAL;
  626. if (macvtap16_to_cpu(q, vnet_hdr.hdr_len) > len)
  627. goto err;
  628. }
  629. err = -EINVAL;
  630. if (unlikely(len < ETH_HLEN))
  631. goto err;
  632. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
  633. struct iov_iter i;
  634. copylen = vnet_hdr.hdr_len ?
  635. macvtap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
  636. if (copylen > good_linear)
  637. copylen = good_linear;
  638. linear = copylen;
  639. i = *from;
  640. iov_iter_advance(&i, copylen);
  641. if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
  642. zerocopy = true;
  643. }
  644. if (!zerocopy) {
  645. copylen = len;
  646. if (macvtap16_to_cpu(q, vnet_hdr.hdr_len) > good_linear)
  647. linear = good_linear;
  648. else
  649. linear = macvtap16_to_cpu(q, vnet_hdr.hdr_len);
  650. }
  651. skb = macvtap_alloc_skb(&q->sk, MACVTAP_RESERVE, copylen,
  652. linear, noblock, &err);
  653. if (!skb)
  654. goto err;
  655. if (zerocopy)
  656. err = zerocopy_sg_from_iter(skb, from);
  657. else {
  658. err = skb_copy_datagram_from_iter(skb, 0, from, len);
  659. if (!err && m && m->msg_control) {
  660. struct ubuf_info *uarg = m->msg_control;
  661. uarg->callback(uarg, false);
  662. }
  663. }
  664. if (err)
  665. goto err_kfree;
  666. skb_set_network_header(skb, ETH_HLEN);
  667. skb_reset_mac_header(skb);
  668. skb->protocol = eth_hdr(skb)->h_proto;
  669. if (vnet_hdr_len) {
  670. err = macvtap_skb_from_vnet_hdr(q, skb, &vnet_hdr);
  671. if (err)
  672. goto err_kfree;
  673. }
  674. skb_probe_transport_header(skb, ETH_HLEN);
  675. /* Move network header to the right position for VLAN tagged packets */
  676. if ((skb->protocol == htons(ETH_P_8021Q) ||
  677. skb->protocol == htons(ETH_P_8021AD)) &&
  678. __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
  679. skb_set_network_header(skb, depth);
  680. rcu_read_lock();
  681. vlan = rcu_dereference(q->vlan);
  682. /* copy skb_ubuf_info for callback when skb has no error */
  683. if (zerocopy) {
  684. skb_shinfo(skb)->destructor_arg = m->msg_control;
  685. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  686. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  687. }
  688. if (vlan) {
  689. skb->dev = vlan->dev;
  690. dev_queue_xmit(skb);
  691. } else {
  692. kfree_skb(skb);
  693. }
  694. rcu_read_unlock();
  695. return total_len;
  696. err_kfree:
  697. kfree_skb(skb);
  698. err:
  699. rcu_read_lock();
  700. vlan = rcu_dereference(q->vlan);
  701. if (vlan)
  702. this_cpu_inc(vlan->pcpu_stats->tx_dropped);
  703. rcu_read_unlock();
  704. return err;
  705. }
  706. static ssize_t macvtap_write_iter(struct kiocb *iocb, struct iov_iter *from)
  707. {
  708. struct file *file = iocb->ki_filp;
  709. struct macvtap_queue *q = file->private_data;
  710. return macvtap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
  711. }
  712. /* Put packet to the user space buffer */
  713. static ssize_t macvtap_put_user(struct macvtap_queue *q,
  714. const struct sk_buff *skb,
  715. struct iov_iter *iter)
  716. {
  717. int ret;
  718. int vnet_hdr_len = 0;
  719. int vlan_offset = 0;
  720. int total;
  721. if (q->flags & IFF_VNET_HDR) {
  722. struct virtio_net_hdr vnet_hdr;
  723. vnet_hdr_len = q->vnet_hdr_sz;
  724. if (iov_iter_count(iter) < vnet_hdr_len)
  725. return -EINVAL;
  726. macvtap_skb_to_vnet_hdr(q, skb, &vnet_hdr);
  727. if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
  728. sizeof(vnet_hdr))
  729. return -EFAULT;
  730. iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
  731. }
  732. total = vnet_hdr_len;
  733. total += skb->len;
  734. if (skb_vlan_tag_present(skb)) {
  735. struct {
  736. __be16 h_vlan_proto;
  737. __be16 h_vlan_TCI;
  738. } veth;
  739. veth.h_vlan_proto = skb->vlan_proto;
  740. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  741. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  742. total += VLAN_HLEN;
  743. ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
  744. if (ret || !iov_iter_count(iter))
  745. goto done;
  746. ret = copy_to_iter(&veth, sizeof(veth), iter);
  747. if (ret != sizeof(veth) || !iov_iter_count(iter))
  748. goto done;
  749. }
  750. ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
  751. skb->len - vlan_offset);
  752. done:
  753. return ret ? ret : total;
  754. }
  755. static ssize_t macvtap_do_read(struct macvtap_queue *q,
  756. struct iov_iter *to,
  757. int noblock)
  758. {
  759. DEFINE_WAIT(wait);
  760. struct sk_buff *skb;
  761. ssize_t ret = 0;
  762. if (!iov_iter_count(to))
  763. return 0;
  764. while (1) {
  765. if (!noblock)
  766. prepare_to_wait(sk_sleep(&q->sk), &wait,
  767. TASK_INTERRUPTIBLE);
  768. /* Read frames from the queue */
  769. skb = skb_dequeue(&q->sk.sk_receive_queue);
  770. if (skb)
  771. break;
  772. if (noblock) {
  773. ret = -EAGAIN;
  774. break;
  775. }
  776. if (signal_pending(current)) {
  777. ret = -ERESTARTSYS;
  778. break;
  779. }
  780. /* Nothing to read, let's sleep */
  781. schedule();
  782. }
  783. if (skb) {
  784. ret = macvtap_put_user(q, skb, to);
  785. if (unlikely(ret < 0))
  786. kfree_skb(skb);
  787. else
  788. consume_skb(skb);
  789. }
  790. if (!noblock)
  791. finish_wait(sk_sleep(&q->sk), &wait);
  792. return ret;
  793. }
  794. static ssize_t macvtap_read_iter(struct kiocb *iocb, struct iov_iter *to)
  795. {
  796. struct file *file = iocb->ki_filp;
  797. struct macvtap_queue *q = file->private_data;
  798. ssize_t len = iov_iter_count(to), ret;
  799. ret = macvtap_do_read(q, to, file->f_flags & O_NONBLOCK);
  800. ret = min_t(ssize_t, ret, len);
  801. if (ret > 0)
  802. iocb->ki_pos = ret;
  803. return ret;
  804. }
  805. static struct macvlan_dev *macvtap_get_vlan(struct macvtap_queue *q)
  806. {
  807. struct macvlan_dev *vlan;
  808. ASSERT_RTNL();
  809. vlan = rtnl_dereference(q->vlan);
  810. if (vlan)
  811. dev_hold(vlan->dev);
  812. return vlan;
  813. }
  814. static void macvtap_put_vlan(struct macvlan_dev *vlan)
  815. {
  816. dev_put(vlan->dev);
  817. }
  818. static int macvtap_ioctl_set_queue(struct file *file, unsigned int flags)
  819. {
  820. struct macvtap_queue *q = file->private_data;
  821. struct macvlan_dev *vlan;
  822. int ret;
  823. vlan = macvtap_get_vlan(q);
  824. if (!vlan)
  825. return -EINVAL;
  826. if (flags & IFF_ATTACH_QUEUE)
  827. ret = macvtap_enable_queue(vlan->dev, file, q);
  828. else if (flags & IFF_DETACH_QUEUE)
  829. ret = macvtap_disable_queue(q);
  830. else
  831. ret = -EINVAL;
  832. macvtap_put_vlan(vlan);
  833. return ret;
  834. }
  835. static int set_offload(struct macvtap_queue *q, unsigned long arg)
  836. {
  837. struct macvlan_dev *vlan;
  838. netdev_features_t features;
  839. netdev_features_t feature_mask = 0;
  840. vlan = rtnl_dereference(q->vlan);
  841. if (!vlan)
  842. return -ENOLINK;
  843. features = vlan->dev->features;
  844. if (arg & TUN_F_CSUM) {
  845. feature_mask = NETIF_F_HW_CSUM;
  846. if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
  847. if (arg & TUN_F_TSO_ECN)
  848. feature_mask |= NETIF_F_TSO_ECN;
  849. if (arg & TUN_F_TSO4)
  850. feature_mask |= NETIF_F_TSO;
  851. if (arg & TUN_F_TSO6)
  852. feature_mask |= NETIF_F_TSO6;
  853. }
  854. if (arg & TUN_F_UFO)
  855. feature_mask |= NETIF_F_UFO;
  856. }
  857. /* tun/tap driver inverts the usage for TSO offloads, where
  858. * setting the TSO bit means that the userspace wants to
  859. * accept TSO frames and turning it off means that user space
  860. * does not support TSO.
  861. * For macvtap, we have to invert it to mean the same thing.
  862. * When user space turns off TSO, we turn off GSO/LRO so that
  863. * user-space will not receive TSO frames.
  864. */
  865. if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_UFO))
  866. features |= RX_OFFLOADS;
  867. else
  868. features &= ~RX_OFFLOADS;
  869. /* tap_features are the same as features on tun/tap and
  870. * reflect user expectations.
  871. */
  872. vlan->tap_features = feature_mask;
  873. vlan->set_features = features;
  874. netdev_update_features(vlan->dev);
  875. return 0;
  876. }
  877. /*
  878. * provide compatibility with generic tun/tap interface
  879. */
  880. static long macvtap_ioctl(struct file *file, unsigned int cmd,
  881. unsigned long arg)
  882. {
  883. struct macvtap_queue *q = file->private_data;
  884. struct macvlan_dev *vlan;
  885. void __user *argp = (void __user *)arg;
  886. struct ifreq __user *ifr = argp;
  887. unsigned int __user *up = argp;
  888. unsigned short u;
  889. int __user *sp = argp;
  890. struct sockaddr sa;
  891. int s;
  892. int ret;
  893. switch (cmd) {
  894. case TUNSETIFF:
  895. /* ignore the name, just look at flags */
  896. if (get_user(u, &ifr->ifr_flags))
  897. return -EFAULT;
  898. ret = 0;
  899. if ((u & ~MACVTAP_FEATURES) != (IFF_NO_PI | IFF_TAP))
  900. ret = -EINVAL;
  901. else
  902. q->flags = (q->flags & ~MACVTAP_FEATURES) | u;
  903. return ret;
  904. case TUNGETIFF:
  905. rtnl_lock();
  906. vlan = macvtap_get_vlan(q);
  907. if (!vlan) {
  908. rtnl_unlock();
  909. return -ENOLINK;
  910. }
  911. ret = 0;
  912. u = q->flags;
  913. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  914. put_user(u, &ifr->ifr_flags))
  915. ret = -EFAULT;
  916. macvtap_put_vlan(vlan);
  917. rtnl_unlock();
  918. return ret;
  919. case TUNSETQUEUE:
  920. if (get_user(u, &ifr->ifr_flags))
  921. return -EFAULT;
  922. rtnl_lock();
  923. ret = macvtap_ioctl_set_queue(file, u);
  924. rtnl_unlock();
  925. return ret;
  926. case TUNGETFEATURES:
  927. if (put_user(IFF_TAP | IFF_NO_PI | MACVTAP_FEATURES, up))
  928. return -EFAULT;
  929. return 0;
  930. case TUNSETSNDBUF:
  931. if (get_user(s, sp))
  932. return -EFAULT;
  933. q->sk.sk_sndbuf = s;
  934. return 0;
  935. case TUNGETVNETHDRSZ:
  936. s = q->vnet_hdr_sz;
  937. if (put_user(s, sp))
  938. return -EFAULT;
  939. return 0;
  940. case TUNSETVNETHDRSZ:
  941. if (get_user(s, sp))
  942. return -EFAULT;
  943. if (s < (int)sizeof(struct virtio_net_hdr))
  944. return -EINVAL;
  945. q->vnet_hdr_sz = s;
  946. return 0;
  947. case TUNGETVNETLE:
  948. s = !!(q->flags & MACVTAP_VNET_LE);
  949. if (put_user(s, sp))
  950. return -EFAULT;
  951. return 0;
  952. case TUNSETVNETLE:
  953. if (get_user(s, sp))
  954. return -EFAULT;
  955. if (s)
  956. q->flags |= MACVTAP_VNET_LE;
  957. else
  958. q->flags &= ~MACVTAP_VNET_LE;
  959. return 0;
  960. case TUNGETVNETBE:
  961. return macvtap_get_vnet_be(q, sp);
  962. case TUNSETVNETBE:
  963. return macvtap_set_vnet_be(q, sp);
  964. case TUNSETOFFLOAD:
  965. /* let the user check for future flags */
  966. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  967. TUN_F_TSO_ECN | TUN_F_UFO))
  968. return -EINVAL;
  969. rtnl_lock();
  970. ret = set_offload(q, arg);
  971. rtnl_unlock();
  972. return ret;
  973. case SIOCGIFHWADDR:
  974. rtnl_lock();
  975. vlan = macvtap_get_vlan(q);
  976. if (!vlan) {
  977. rtnl_unlock();
  978. return -ENOLINK;
  979. }
  980. ret = 0;
  981. u = vlan->dev->type;
  982. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  983. copy_to_user(&ifr->ifr_hwaddr.sa_data, vlan->dev->dev_addr, ETH_ALEN) ||
  984. put_user(u, &ifr->ifr_hwaddr.sa_family))
  985. ret = -EFAULT;
  986. macvtap_put_vlan(vlan);
  987. rtnl_unlock();
  988. return ret;
  989. case SIOCSIFHWADDR:
  990. if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
  991. return -EFAULT;
  992. rtnl_lock();
  993. vlan = macvtap_get_vlan(q);
  994. if (!vlan) {
  995. rtnl_unlock();
  996. return -ENOLINK;
  997. }
  998. ret = dev_set_mac_address(vlan->dev, &sa);
  999. macvtap_put_vlan(vlan);
  1000. rtnl_unlock();
  1001. return ret;
  1002. default:
  1003. return -EINVAL;
  1004. }
  1005. }
  1006. #ifdef CONFIG_COMPAT
  1007. static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
  1008. unsigned long arg)
  1009. {
  1010. return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  1011. }
  1012. #endif
  1013. static const struct file_operations macvtap_fops = {
  1014. .owner = THIS_MODULE,
  1015. .open = macvtap_open,
  1016. .release = macvtap_release,
  1017. .read_iter = macvtap_read_iter,
  1018. .write_iter = macvtap_write_iter,
  1019. .poll = macvtap_poll,
  1020. .llseek = no_llseek,
  1021. .unlocked_ioctl = macvtap_ioctl,
  1022. #ifdef CONFIG_COMPAT
  1023. .compat_ioctl = macvtap_compat_ioctl,
  1024. #endif
  1025. };
  1026. static int macvtap_sendmsg(struct socket *sock, struct msghdr *m,
  1027. size_t total_len)
  1028. {
  1029. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  1030. return macvtap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT);
  1031. }
  1032. static int macvtap_recvmsg(struct socket *sock, struct msghdr *m,
  1033. size_t total_len, int flags)
  1034. {
  1035. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  1036. int ret;
  1037. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  1038. return -EINVAL;
  1039. ret = macvtap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT);
  1040. if (ret > total_len) {
  1041. m->msg_flags |= MSG_TRUNC;
  1042. ret = flags & MSG_TRUNC ? ret : total_len;
  1043. }
  1044. return ret;
  1045. }
  1046. /* Ops structure to mimic raw sockets with tun */
  1047. static const struct proto_ops macvtap_socket_ops = {
  1048. .sendmsg = macvtap_sendmsg,
  1049. .recvmsg = macvtap_recvmsg,
  1050. };
  1051. /* Get an underlying socket object from tun file. Returns error unless file is
  1052. * attached to a device. The returned object works like a packet socket, it
  1053. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1054. * holding a reference to the file for as long as the socket is in use. */
  1055. struct socket *macvtap_get_socket(struct file *file)
  1056. {
  1057. struct macvtap_queue *q;
  1058. if (file->f_op != &macvtap_fops)
  1059. return ERR_PTR(-EINVAL);
  1060. q = file->private_data;
  1061. if (!q)
  1062. return ERR_PTR(-EBADFD);
  1063. return &q->sock;
  1064. }
  1065. EXPORT_SYMBOL_GPL(macvtap_get_socket);
  1066. static int macvtap_device_event(struct notifier_block *unused,
  1067. unsigned long event, void *ptr)
  1068. {
  1069. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1070. struct macvlan_dev *vlan;
  1071. struct device *classdev;
  1072. dev_t devt;
  1073. int err;
  1074. if (dev->rtnl_link_ops != &macvtap_link_ops)
  1075. return NOTIFY_DONE;
  1076. vlan = netdev_priv(dev);
  1077. switch (event) {
  1078. case NETDEV_REGISTER:
  1079. /* Create the device node here after the network device has
  1080. * been registered but before register_netdevice has
  1081. * finished running.
  1082. */
  1083. err = macvtap_get_minor(vlan);
  1084. if (err)
  1085. return notifier_from_errno(err);
  1086. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  1087. classdev = device_create(macvtap_class, &dev->dev, devt,
  1088. dev, "tap%d", dev->ifindex);
  1089. if (IS_ERR(classdev)) {
  1090. macvtap_free_minor(vlan);
  1091. return notifier_from_errno(PTR_ERR(classdev));
  1092. }
  1093. break;
  1094. case NETDEV_UNREGISTER:
  1095. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  1096. device_destroy(macvtap_class, devt);
  1097. macvtap_free_minor(vlan);
  1098. break;
  1099. }
  1100. return NOTIFY_DONE;
  1101. }
  1102. static struct notifier_block macvtap_notifier_block __read_mostly = {
  1103. .notifier_call = macvtap_device_event,
  1104. };
  1105. static int macvtap_init(void)
  1106. {
  1107. int err;
  1108. err = alloc_chrdev_region(&macvtap_major, 0,
  1109. MACVTAP_NUM_DEVS, "macvtap");
  1110. if (err)
  1111. goto out1;
  1112. cdev_init(&macvtap_cdev, &macvtap_fops);
  1113. err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
  1114. if (err)
  1115. goto out2;
  1116. macvtap_class = class_create(THIS_MODULE, "macvtap");
  1117. if (IS_ERR(macvtap_class)) {
  1118. err = PTR_ERR(macvtap_class);
  1119. goto out3;
  1120. }
  1121. err = register_netdevice_notifier(&macvtap_notifier_block);
  1122. if (err)
  1123. goto out4;
  1124. err = macvlan_link_register(&macvtap_link_ops);
  1125. if (err)
  1126. goto out5;
  1127. return 0;
  1128. out5:
  1129. unregister_netdevice_notifier(&macvtap_notifier_block);
  1130. out4:
  1131. class_unregister(macvtap_class);
  1132. out3:
  1133. cdev_del(&macvtap_cdev);
  1134. out2:
  1135. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1136. out1:
  1137. return err;
  1138. }
  1139. module_init(macvtap_init);
  1140. static void macvtap_exit(void)
  1141. {
  1142. rtnl_link_unregister(&macvtap_link_ops);
  1143. unregister_netdevice_notifier(&macvtap_notifier_block);
  1144. class_unregister(macvtap_class);
  1145. cdev_del(&macvtap_cdev);
  1146. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1147. idr_destroy(&minor_idr);
  1148. }
  1149. module_exit(macvtap_exit);
  1150. MODULE_ALIAS_RTNL_LINK("macvtap");
  1151. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  1152. MODULE_LICENSE("GPL");