macvtap.c 31 KB

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