macvtap.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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(SOCKWQ_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(SOCKWQ_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 = READ_ONCE(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. else if (copylen < ETH_HLEN)
  639. copylen = ETH_HLEN;
  640. linear = copylen;
  641. i = *from;
  642. iov_iter_advance(&i, copylen);
  643. if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
  644. zerocopy = true;
  645. }
  646. if (!zerocopy) {
  647. copylen = len;
  648. linear = macvtap16_to_cpu(q, vnet_hdr.hdr_len);
  649. if (linear > good_linear)
  650. linear = good_linear;
  651. else if (linear < ETH_HLEN)
  652. linear = ETH_HLEN;
  653. }
  654. skb = macvtap_alloc_skb(&q->sk, MACVTAP_RESERVE, copylen,
  655. linear, noblock, &err);
  656. if (!skb)
  657. goto err;
  658. if (zerocopy)
  659. err = zerocopy_sg_from_iter(skb, from);
  660. else {
  661. err = skb_copy_datagram_from_iter(skb, 0, from, len);
  662. if (!err && m && m->msg_control) {
  663. struct ubuf_info *uarg = m->msg_control;
  664. uarg->callback(uarg, false);
  665. }
  666. }
  667. if (err)
  668. goto err_kfree;
  669. skb_set_network_header(skb, ETH_HLEN);
  670. skb_reset_mac_header(skb);
  671. skb->protocol = eth_hdr(skb)->h_proto;
  672. if (vnet_hdr_len) {
  673. err = macvtap_skb_from_vnet_hdr(q, skb, &vnet_hdr);
  674. if (err)
  675. goto err_kfree;
  676. }
  677. skb_probe_transport_header(skb, ETH_HLEN);
  678. /* Move network header to the right position for VLAN tagged packets */
  679. if ((skb->protocol == htons(ETH_P_8021Q) ||
  680. skb->protocol == htons(ETH_P_8021AD)) &&
  681. __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
  682. skb_set_network_header(skb, depth);
  683. rcu_read_lock();
  684. vlan = rcu_dereference(q->vlan);
  685. /* copy skb_ubuf_info for callback when skb has no error */
  686. if (zerocopy) {
  687. skb_shinfo(skb)->destructor_arg = m->msg_control;
  688. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  689. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  690. }
  691. if (vlan) {
  692. skb->dev = vlan->dev;
  693. dev_queue_xmit(skb);
  694. } else {
  695. kfree_skb(skb);
  696. }
  697. rcu_read_unlock();
  698. return total_len;
  699. err_kfree:
  700. kfree_skb(skb);
  701. err:
  702. rcu_read_lock();
  703. vlan = rcu_dereference(q->vlan);
  704. if (vlan)
  705. this_cpu_inc(vlan->pcpu_stats->tx_dropped);
  706. rcu_read_unlock();
  707. return err;
  708. }
  709. static ssize_t macvtap_write_iter(struct kiocb *iocb, struct iov_iter *from)
  710. {
  711. struct file *file = iocb->ki_filp;
  712. struct macvtap_queue *q = file->private_data;
  713. return macvtap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
  714. }
  715. /* Put packet to the user space buffer */
  716. static ssize_t macvtap_put_user(struct macvtap_queue *q,
  717. const struct sk_buff *skb,
  718. struct iov_iter *iter)
  719. {
  720. int ret;
  721. int vnet_hdr_len = 0;
  722. int vlan_offset = 0;
  723. int total;
  724. if (q->flags & IFF_VNET_HDR) {
  725. struct virtio_net_hdr vnet_hdr;
  726. vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
  727. if (iov_iter_count(iter) < vnet_hdr_len)
  728. return -EINVAL;
  729. macvtap_skb_to_vnet_hdr(q, skb, &vnet_hdr);
  730. if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
  731. sizeof(vnet_hdr))
  732. return -EFAULT;
  733. iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
  734. }
  735. total = vnet_hdr_len;
  736. total += skb->len;
  737. if (skb_vlan_tag_present(skb)) {
  738. struct {
  739. __be16 h_vlan_proto;
  740. __be16 h_vlan_TCI;
  741. } veth;
  742. veth.h_vlan_proto = skb->vlan_proto;
  743. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  744. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  745. total += VLAN_HLEN;
  746. ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
  747. if (ret || !iov_iter_count(iter))
  748. goto done;
  749. ret = copy_to_iter(&veth, sizeof(veth), iter);
  750. if (ret != sizeof(veth) || !iov_iter_count(iter))
  751. goto done;
  752. }
  753. ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
  754. skb->len - vlan_offset);
  755. done:
  756. return ret ? ret : total;
  757. }
  758. static ssize_t macvtap_do_read(struct macvtap_queue *q,
  759. struct iov_iter *to,
  760. int noblock)
  761. {
  762. DEFINE_WAIT(wait);
  763. struct sk_buff *skb;
  764. ssize_t ret = 0;
  765. if (!iov_iter_count(to))
  766. return 0;
  767. while (1) {
  768. if (!noblock)
  769. prepare_to_wait(sk_sleep(&q->sk), &wait,
  770. TASK_INTERRUPTIBLE);
  771. /* Read frames from the queue */
  772. skb = skb_dequeue(&q->sk.sk_receive_queue);
  773. if (skb)
  774. break;
  775. if (noblock) {
  776. ret = -EAGAIN;
  777. break;
  778. }
  779. if (signal_pending(current)) {
  780. ret = -ERESTARTSYS;
  781. break;
  782. }
  783. /* Nothing to read, let's sleep */
  784. schedule();
  785. }
  786. if (!noblock)
  787. finish_wait(sk_sleep(&q->sk), &wait);
  788. if (skb) {
  789. ret = macvtap_put_user(q, skb, to);
  790. if (unlikely(ret < 0))
  791. kfree_skb(skb);
  792. else
  793. consume_skb(skb);
  794. }
  795. return ret;
  796. }
  797. static ssize_t macvtap_read_iter(struct kiocb *iocb, struct iov_iter *to)
  798. {
  799. struct file *file = iocb->ki_filp;
  800. struct macvtap_queue *q = file->private_data;
  801. ssize_t len = iov_iter_count(to), ret;
  802. ret = macvtap_do_read(q, to, file->f_flags & O_NONBLOCK);
  803. ret = min_t(ssize_t, ret, len);
  804. if (ret > 0)
  805. iocb->ki_pos = ret;
  806. return ret;
  807. }
  808. static struct macvlan_dev *macvtap_get_vlan(struct macvtap_queue *q)
  809. {
  810. struct macvlan_dev *vlan;
  811. ASSERT_RTNL();
  812. vlan = rtnl_dereference(q->vlan);
  813. if (vlan)
  814. dev_hold(vlan->dev);
  815. return vlan;
  816. }
  817. static void macvtap_put_vlan(struct macvlan_dev *vlan)
  818. {
  819. dev_put(vlan->dev);
  820. }
  821. static int macvtap_ioctl_set_queue(struct file *file, unsigned int flags)
  822. {
  823. struct macvtap_queue *q = file->private_data;
  824. struct macvlan_dev *vlan;
  825. int ret;
  826. vlan = macvtap_get_vlan(q);
  827. if (!vlan)
  828. return -EINVAL;
  829. if (flags & IFF_ATTACH_QUEUE)
  830. ret = macvtap_enable_queue(vlan->dev, file, q);
  831. else if (flags & IFF_DETACH_QUEUE)
  832. ret = macvtap_disable_queue(q);
  833. else
  834. ret = -EINVAL;
  835. macvtap_put_vlan(vlan);
  836. return ret;
  837. }
  838. static int set_offload(struct macvtap_queue *q, unsigned long arg)
  839. {
  840. struct macvlan_dev *vlan;
  841. netdev_features_t features;
  842. netdev_features_t feature_mask = 0;
  843. vlan = rtnl_dereference(q->vlan);
  844. if (!vlan)
  845. return -ENOLINK;
  846. features = vlan->dev->features;
  847. if (arg & TUN_F_CSUM) {
  848. feature_mask = NETIF_F_HW_CSUM;
  849. if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
  850. if (arg & TUN_F_TSO_ECN)
  851. feature_mask |= NETIF_F_TSO_ECN;
  852. if (arg & TUN_F_TSO4)
  853. feature_mask |= NETIF_F_TSO;
  854. if (arg & TUN_F_TSO6)
  855. feature_mask |= NETIF_F_TSO6;
  856. }
  857. if (arg & TUN_F_UFO)
  858. feature_mask |= NETIF_F_UFO;
  859. }
  860. /* tun/tap driver inverts the usage for TSO offloads, where
  861. * setting the TSO bit means that the userspace wants to
  862. * accept TSO frames and turning it off means that user space
  863. * does not support TSO.
  864. * For macvtap, we have to invert it to mean the same thing.
  865. * When user space turns off TSO, we turn off GSO/LRO so that
  866. * user-space will not receive TSO frames.
  867. */
  868. if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_UFO))
  869. features |= RX_OFFLOADS;
  870. else
  871. features &= ~RX_OFFLOADS;
  872. /* tap_features are the same as features on tun/tap and
  873. * reflect user expectations.
  874. */
  875. vlan->tap_features = feature_mask;
  876. vlan->set_features = features;
  877. netdev_update_features(vlan->dev);
  878. return 0;
  879. }
  880. /*
  881. * provide compatibility with generic tun/tap interface
  882. */
  883. static long macvtap_ioctl(struct file *file, unsigned int cmd,
  884. unsigned long arg)
  885. {
  886. struct macvtap_queue *q = file->private_data;
  887. struct macvlan_dev *vlan;
  888. void __user *argp = (void __user *)arg;
  889. struct ifreq __user *ifr = argp;
  890. unsigned int __user *up = argp;
  891. unsigned short u;
  892. int __user *sp = argp;
  893. struct sockaddr sa;
  894. int s;
  895. int ret;
  896. switch (cmd) {
  897. case TUNSETIFF:
  898. /* ignore the name, just look at flags */
  899. if (get_user(u, &ifr->ifr_flags))
  900. return -EFAULT;
  901. ret = 0;
  902. if ((u & ~MACVTAP_FEATURES) != (IFF_NO_PI | IFF_TAP))
  903. ret = -EINVAL;
  904. else
  905. q->flags = (q->flags & ~MACVTAP_FEATURES) | u;
  906. return ret;
  907. case TUNGETIFF:
  908. rtnl_lock();
  909. vlan = macvtap_get_vlan(q);
  910. if (!vlan) {
  911. rtnl_unlock();
  912. return -ENOLINK;
  913. }
  914. ret = 0;
  915. u = q->flags;
  916. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  917. put_user(u, &ifr->ifr_flags))
  918. ret = -EFAULT;
  919. macvtap_put_vlan(vlan);
  920. rtnl_unlock();
  921. return ret;
  922. case TUNSETQUEUE:
  923. if (get_user(u, &ifr->ifr_flags))
  924. return -EFAULT;
  925. rtnl_lock();
  926. ret = macvtap_ioctl_set_queue(file, u);
  927. rtnl_unlock();
  928. return ret;
  929. case TUNGETFEATURES:
  930. if (put_user(IFF_TAP | IFF_NO_PI | MACVTAP_FEATURES, up))
  931. return -EFAULT;
  932. return 0;
  933. case TUNSETSNDBUF:
  934. if (get_user(s, sp))
  935. return -EFAULT;
  936. if (s <= 0)
  937. return -EINVAL;
  938. q->sk.sk_sndbuf = s;
  939. return 0;
  940. case TUNGETVNETHDRSZ:
  941. s = q->vnet_hdr_sz;
  942. if (put_user(s, sp))
  943. return -EFAULT;
  944. return 0;
  945. case TUNSETVNETHDRSZ:
  946. if (get_user(s, sp))
  947. return -EFAULT;
  948. if (s < (int)sizeof(struct virtio_net_hdr))
  949. return -EINVAL;
  950. q->vnet_hdr_sz = s;
  951. return 0;
  952. case TUNGETVNETLE:
  953. s = !!(q->flags & MACVTAP_VNET_LE);
  954. if (put_user(s, sp))
  955. return -EFAULT;
  956. return 0;
  957. case TUNSETVNETLE:
  958. if (get_user(s, sp))
  959. return -EFAULT;
  960. if (s)
  961. q->flags |= MACVTAP_VNET_LE;
  962. else
  963. q->flags &= ~MACVTAP_VNET_LE;
  964. return 0;
  965. case TUNGETVNETBE:
  966. return macvtap_get_vnet_be(q, sp);
  967. case TUNSETVNETBE:
  968. return macvtap_set_vnet_be(q, sp);
  969. case TUNSETOFFLOAD:
  970. /* let the user check for future flags */
  971. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  972. TUN_F_TSO_ECN | TUN_F_UFO))
  973. return -EINVAL;
  974. rtnl_lock();
  975. ret = set_offload(q, arg);
  976. rtnl_unlock();
  977. return ret;
  978. case SIOCGIFHWADDR:
  979. rtnl_lock();
  980. vlan = macvtap_get_vlan(q);
  981. if (!vlan) {
  982. rtnl_unlock();
  983. return -ENOLINK;
  984. }
  985. ret = 0;
  986. u = vlan->dev->type;
  987. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  988. copy_to_user(&ifr->ifr_hwaddr.sa_data, vlan->dev->dev_addr, ETH_ALEN) ||
  989. put_user(u, &ifr->ifr_hwaddr.sa_family))
  990. ret = -EFAULT;
  991. macvtap_put_vlan(vlan);
  992. rtnl_unlock();
  993. return ret;
  994. case SIOCSIFHWADDR:
  995. if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
  996. return -EFAULT;
  997. rtnl_lock();
  998. vlan = macvtap_get_vlan(q);
  999. if (!vlan) {
  1000. rtnl_unlock();
  1001. return -ENOLINK;
  1002. }
  1003. ret = dev_set_mac_address(vlan->dev, &sa);
  1004. macvtap_put_vlan(vlan);
  1005. rtnl_unlock();
  1006. return ret;
  1007. default:
  1008. return -EINVAL;
  1009. }
  1010. }
  1011. #ifdef CONFIG_COMPAT
  1012. static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
  1013. unsigned long arg)
  1014. {
  1015. return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  1016. }
  1017. #endif
  1018. static const struct file_operations macvtap_fops = {
  1019. .owner = THIS_MODULE,
  1020. .open = macvtap_open,
  1021. .release = macvtap_release,
  1022. .read_iter = macvtap_read_iter,
  1023. .write_iter = macvtap_write_iter,
  1024. .poll = macvtap_poll,
  1025. .llseek = no_llseek,
  1026. .unlocked_ioctl = macvtap_ioctl,
  1027. #ifdef CONFIG_COMPAT
  1028. .compat_ioctl = macvtap_compat_ioctl,
  1029. #endif
  1030. };
  1031. static int macvtap_sendmsg(struct socket *sock, struct msghdr *m,
  1032. size_t total_len)
  1033. {
  1034. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  1035. return macvtap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT);
  1036. }
  1037. static int macvtap_recvmsg(struct socket *sock, struct msghdr *m,
  1038. size_t total_len, int flags)
  1039. {
  1040. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  1041. int ret;
  1042. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  1043. return -EINVAL;
  1044. ret = macvtap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT);
  1045. if (ret > total_len) {
  1046. m->msg_flags |= MSG_TRUNC;
  1047. ret = flags & MSG_TRUNC ? ret : total_len;
  1048. }
  1049. return ret;
  1050. }
  1051. /* Ops structure to mimic raw sockets with tun */
  1052. static const struct proto_ops macvtap_socket_ops = {
  1053. .sendmsg = macvtap_sendmsg,
  1054. .recvmsg = macvtap_recvmsg,
  1055. };
  1056. /* Get an underlying socket object from tun file. Returns error unless file is
  1057. * attached to a device. The returned object works like a packet socket, it
  1058. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1059. * holding a reference to the file for as long as the socket is in use. */
  1060. struct socket *macvtap_get_socket(struct file *file)
  1061. {
  1062. struct macvtap_queue *q;
  1063. if (file->f_op != &macvtap_fops)
  1064. return ERR_PTR(-EINVAL);
  1065. q = file->private_data;
  1066. if (!q)
  1067. return ERR_PTR(-EBADFD);
  1068. return &q->sock;
  1069. }
  1070. EXPORT_SYMBOL_GPL(macvtap_get_socket);
  1071. static int macvtap_device_event(struct notifier_block *unused,
  1072. unsigned long event, void *ptr)
  1073. {
  1074. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1075. struct macvlan_dev *vlan;
  1076. struct device *classdev;
  1077. dev_t devt;
  1078. int err;
  1079. if (dev->rtnl_link_ops != &macvtap_link_ops)
  1080. return NOTIFY_DONE;
  1081. vlan = netdev_priv(dev);
  1082. switch (event) {
  1083. case NETDEV_REGISTER:
  1084. /* Create the device node here after the network device has
  1085. * been registered but before register_netdevice has
  1086. * finished running.
  1087. */
  1088. err = macvtap_get_minor(vlan);
  1089. if (err)
  1090. return notifier_from_errno(err);
  1091. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  1092. classdev = device_create(macvtap_class, &dev->dev, devt,
  1093. dev, "tap%d", dev->ifindex);
  1094. if (IS_ERR(classdev)) {
  1095. macvtap_free_minor(vlan);
  1096. return notifier_from_errno(PTR_ERR(classdev));
  1097. }
  1098. break;
  1099. case NETDEV_UNREGISTER:
  1100. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  1101. device_destroy(macvtap_class, devt);
  1102. macvtap_free_minor(vlan);
  1103. break;
  1104. }
  1105. return NOTIFY_DONE;
  1106. }
  1107. static struct notifier_block macvtap_notifier_block __read_mostly = {
  1108. .notifier_call = macvtap_device_event,
  1109. };
  1110. static int macvtap_init(void)
  1111. {
  1112. int err;
  1113. err = alloc_chrdev_region(&macvtap_major, 0,
  1114. MACVTAP_NUM_DEVS, "macvtap");
  1115. if (err)
  1116. goto out1;
  1117. cdev_init(&macvtap_cdev, &macvtap_fops);
  1118. err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
  1119. if (err)
  1120. goto out2;
  1121. macvtap_class = class_create(THIS_MODULE, "macvtap");
  1122. if (IS_ERR(macvtap_class)) {
  1123. err = PTR_ERR(macvtap_class);
  1124. goto out3;
  1125. }
  1126. err = register_netdevice_notifier(&macvtap_notifier_block);
  1127. if (err)
  1128. goto out4;
  1129. err = macvlan_link_register(&macvtap_link_ops);
  1130. if (err)
  1131. goto out5;
  1132. return 0;
  1133. out5:
  1134. unregister_netdevice_notifier(&macvtap_notifier_block);
  1135. out4:
  1136. class_unregister(macvtap_class);
  1137. out3:
  1138. cdev_del(&macvtap_cdev);
  1139. out2:
  1140. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1141. out1:
  1142. return err;
  1143. }
  1144. module_init(macvtap_init);
  1145. static void macvtap_exit(void)
  1146. {
  1147. rtnl_link_unregister(&macvtap_link_ops);
  1148. unregister_netdevice_notifier(&macvtap_notifier_block);
  1149. class_unregister(macvtap_class);
  1150. cdev_del(&macvtap_cdev);
  1151. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1152. idr_destroy(&minor_idr);
  1153. }
  1154. module_exit(macvtap_exit);
  1155. MODULE_ALIAS_RTNL_LINK("macvtap");
  1156. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  1157. MODULE_LICENSE("GPL");