macvtap.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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. for (i = 0; i < num_pages; i++)
  500. put_page(page[i]);
  501. return -EFAULT;
  502. }
  503. truesize = size * PAGE_SIZE;
  504. skb->data_len += len;
  505. skb->len += len;
  506. skb->truesize += truesize;
  507. atomic_add(truesize, &skb->sk->sk_wmem_alloc);
  508. while (len) {
  509. int off = base & ~PAGE_MASK;
  510. int size = min_t(int, len, PAGE_SIZE - off);
  511. __skb_fill_page_desc(skb, i, page[i], off, size);
  512. skb_shinfo(skb)->nr_frags++;
  513. /* increase sk_wmem_alloc */
  514. base += size;
  515. len -= size;
  516. i++;
  517. }
  518. offset = 0;
  519. ++from;
  520. }
  521. return 0;
  522. }
  523. /*
  524. * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  525. * be shared with the tun/tap driver.
  526. */
  527. static int macvtap_skb_from_vnet_hdr(struct sk_buff *skb,
  528. struct virtio_net_hdr *vnet_hdr)
  529. {
  530. unsigned short gso_type = 0;
  531. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  532. switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  533. case VIRTIO_NET_HDR_GSO_TCPV4:
  534. gso_type = SKB_GSO_TCPV4;
  535. break;
  536. case VIRTIO_NET_HDR_GSO_TCPV6:
  537. gso_type = SKB_GSO_TCPV6;
  538. break;
  539. case VIRTIO_NET_HDR_GSO_UDP:
  540. gso_type = SKB_GSO_UDP;
  541. break;
  542. default:
  543. return -EINVAL;
  544. }
  545. if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
  546. gso_type |= SKB_GSO_TCP_ECN;
  547. if (vnet_hdr->gso_size == 0)
  548. return -EINVAL;
  549. }
  550. if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  551. if (!skb_partial_csum_set(skb, vnet_hdr->csum_start,
  552. vnet_hdr->csum_offset))
  553. return -EINVAL;
  554. }
  555. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  556. skb_shinfo(skb)->gso_size = vnet_hdr->gso_size;
  557. skb_shinfo(skb)->gso_type = gso_type;
  558. /* Header must be checked, and gso_segs computed. */
  559. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  560. skb_shinfo(skb)->gso_segs = 0;
  561. }
  562. return 0;
  563. }
  564. static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
  565. struct virtio_net_hdr *vnet_hdr)
  566. {
  567. memset(vnet_hdr, 0, sizeof(*vnet_hdr));
  568. if (skb_is_gso(skb)) {
  569. struct skb_shared_info *sinfo = skb_shinfo(skb);
  570. /* This is a hint as to how much should be linear. */
  571. vnet_hdr->hdr_len = skb_headlen(skb);
  572. vnet_hdr->gso_size = sinfo->gso_size;
  573. if (sinfo->gso_type & SKB_GSO_TCPV4)
  574. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  575. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  576. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  577. else if (sinfo->gso_type & SKB_GSO_UDP)
  578. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  579. else
  580. BUG();
  581. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  582. vnet_hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  583. } else
  584. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  585. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  586. vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  587. vnet_hdr->csum_start = skb_checksum_start_offset(skb);
  588. vnet_hdr->csum_offset = skb->csum_offset;
  589. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  590. vnet_hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
  591. } /* else everything is zero */
  592. return 0;
  593. }
  594. /* Get packet from user space buffer */
  595. static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
  596. const struct iovec *iv, unsigned long total_len,
  597. size_t count, int noblock)
  598. {
  599. struct sk_buff *skb;
  600. struct macvlan_dev *vlan;
  601. unsigned long len = total_len;
  602. int err;
  603. struct virtio_net_hdr vnet_hdr = { 0 };
  604. int vnet_hdr_len = 0;
  605. int copylen = 0;
  606. bool zerocopy = false;
  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 = memcpy_fromiovecend((void *)&vnet_hdr, iv, 0,
  614. sizeof(vnet_hdr));
  615. if (err < 0)
  616. goto err;
  617. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  618. vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
  619. vnet_hdr.hdr_len)
  620. vnet_hdr.hdr_len = vnet_hdr.csum_start +
  621. vnet_hdr.csum_offset + 2;
  622. err = -EINVAL;
  623. if (vnet_hdr.hdr_len > len)
  624. goto err;
  625. }
  626. err = -EINVAL;
  627. if (unlikely(len < ETH_HLEN))
  628. goto err;
  629. err = -EMSGSIZE;
  630. if (unlikely(count > UIO_MAXIOV))
  631. goto err;
  632. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY))
  633. zerocopy = true;
  634. if (zerocopy) {
  635. /* Userspace may produce vectors with count greater than
  636. * MAX_SKB_FRAGS, so we need to linearize parts of the skb
  637. * to let the rest of data to be fit in the frags.
  638. */
  639. if (count > MAX_SKB_FRAGS) {
  640. copylen = iov_length(iv, count - MAX_SKB_FRAGS);
  641. if (copylen < vnet_hdr_len)
  642. copylen = 0;
  643. else
  644. copylen -= vnet_hdr_len;
  645. }
  646. /* There are 256 bytes to be copied in skb, so there is enough
  647. * room for skb expand head in case it is used.
  648. * The rest buffer is mapped from userspace.
  649. */
  650. if (copylen < vnet_hdr.hdr_len)
  651. copylen = vnet_hdr.hdr_len;
  652. if (!copylen)
  653. copylen = GOODCOPY_LEN;
  654. } else
  655. copylen = len;
  656. skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
  657. vnet_hdr.hdr_len, noblock, &err);
  658. if (!skb)
  659. goto err;
  660. if (zerocopy)
  661. err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
  662. else
  663. err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
  664. len);
  665. if (err)
  666. goto err_kfree;
  667. skb_set_network_header(skb, ETH_HLEN);
  668. skb_reset_mac_header(skb);
  669. skb->protocol = eth_hdr(skb)->h_proto;
  670. if (vnet_hdr_len) {
  671. err = macvtap_skb_from_vnet_hdr(skb, &vnet_hdr);
  672. if (err)
  673. goto err_kfree;
  674. }
  675. skb_probe_transport_header(skb, ETH_HLEN);
  676. rcu_read_lock();
  677. vlan = rcu_dereference(q->vlan);
  678. /* copy skb_ubuf_info for callback when skb has no error */
  679. if (zerocopy) {
  680. skb_shinfo(skb)->destructor_arg = m->msg_control;
  681. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  682. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  683. }
  684. if (vlan)
  685. macvlan_start_xmit(skb, vlan->dev);
  686. else
  687. kfree_skb(skb);
  688. rcu_read_unlock();
  689. return total_len;
  690. err_kfree:
  691. kfree_skb(skb);
  692. err:
  693. rcu_read_lock();
  694. vlan = rcu_dereference(q->vlan);
  695. if (vlan)
  696. vlan->dev->stats.tx_dropped++;
  697. rcu_read_unlock();
  698. return err;
  699. }
  700. static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
  701. unsigned long count, loff_t pos)
  702. {
  703. struct file *file = iocb->ki_filp;
  704. ssize_t result = -ENOLINK;
  705. struct macvtap_queue *q = file->private_data;
  706. result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
  707. file->f_flags & O_NONBLOCK);
  708. return result;
  709. }
  710. /* Put packet to the user space buffer */
  711. static ssize_t macvtap_put_user(struct macvtap_queue *q,
  712. const struct sk_buff *skb,
  713. const struct iovec *iv, int len)
  714. {
  715. struct macvlan_dev *vlan;
  716. int ret;
  717. int vnet_hdr_len = 0;
  718. int vlan_offset = 0;
  719. int copied;
  720. if (q->flags & IFF_VNET_HDR) {
  721. struct virtio_net_hdr vnet_hdr;
  722. vnet_hdr_len = q->vnet_hdr_sz;
  723. if ((len -= vnet_hdr_len) < 0)
  724. return -EINVAL;
  725. ret = macvtap_skb_to_vnet_hdr(skb, &vnet_hdr);
  726. if (ret)
  727. return ret;
  728. if (memcpy_toiovecend(iv, (void *)&vnet_hdr, 0, sizeof(vnet_hdr)))
  729. return -EFAULT;
  730. }
  731. copied = vnet_hdr_len;
  732. if (!vlan_tx_tag_present(skb))
  733. len = min_t(int, skb->len, len);
  734. else {
  735. int copy;
  736. struct {
  737. __be16 h_vlan_proto;
  738. __be16 h_vlan_TCI;
  739. } veth;
  740. veth.h_vlan_proto = htons(ETH_P_8021Q);
  741. veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
  742. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  743. len = min_t(int, skb->len + VLAN_HLEN, len);
  744. copy = min_t(int, vlan_offset, len);
  745. ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy);
  746. len -= copy;
  747. copied += copy;
  748. if (ret || !len)
  749. goto done;
  750. copy = min_t(int, sizeof(veth), len);
  751. ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy);
  752. len -= copy;
  753. copied += copy;
  754. if (ret || !len)
  755. goto done;
  756. }
  757. ret = skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len);
  758. copied += len;
  759. done:
  760. rcu_read_lock();
  761. vlan = rcu_dereference(q->vlan);
  762. if (vlan)
  763. macvlan_count_rx(vlan, copied - vnet_hdr_len, ret == 0, 0);
  764. rcu_read_unlock();
  765. return ret ? ret : copied;
  766. }
  767. static ssize_t macvtap_do_read(struct macvtap_queue *q, struct kiocb *iocb,
  768. const struct iovec *iv, unsigned long len,
  769. int noblock)
  770. {
  771. DEFINE_WAIT(wait);
  772. struct sk_buff *skb;
  773. ssize_t ret = 0;
  774. while (len) {
  775. if (!noblock)
  776. prepare_to_wait(sk_sleep(&q->sk), &wait,
  777. TASK_INTERRUPTIBLE);
  778. /* Read frames from the queue */
  779. skb = skb_dequeue(&q->sk.sk_receive_queue);
  780. if (!skb) {
  781. if (noblock) {
  782. ret = -EAGAIN;
  783. break;
  784. }
  785. if (signal_pending(current)) {
  786. ret = -ERESTARTSYS;
  787. break;
  788. }
  789. /* Nothing to read, let's sleep */
  790. schedule();
  791. continue;
  792. }
  793. ret = macvtap_put_user(q, skb, iv, len);
  794. kfree_skb(skb);
  795. break;
  796. }
  797. if (!noblock)
  798. finish_wait(sk_sleep(&q->sk), &wait);
  799. return ret;
  800. }
  801. static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
  802. unsigned long count, loff_t pos)
  803. {
  804. struct file *file = iocb->ki_filp;
  805. struct macvtap_queue *q = file->private_data;
  806. ssize_t len, ret = 0;
  807. len = iov_length(iv, count);
  808. if (len < 0) {
  809. ret = -EINVAL;
  810. goto out;
  811. }
  812. ret = macvtap_do_read(q, iocb, iv, len, file->f_flags & O_NONBLOCK);
  813. ret = min_t(ssize_t, ret, len); /* XXX copied from tun.c. Why? */
  814. out:
  815. return ret;
  816. }
  817. static struct macvlan_dev *macvtap_get_vlan(struct macvtap_queue *q)
  818. {
  819. struct macvlan_dev *vlan;
  820. ASSERT_RTNL();
  821. vlan = rtnl_dereference(q->vlan);
  822. if (vlan)
  823. dev_hold(vlan->dev);
  824. return vlan;
  825. }
  826. static void macvtap_put_vlan(struct macvlan_dev *vlan)
  827. {
  828. dev_put(vlan->dev);
  829. }
  830. static int macvtap_ioctl_set_queue(struct file *file, unsigned int flags)
  831. {
  832. struct macvtap_queue *q = file->private_data;
  833. struct macvlan_dev *vlan;
  834. int ret;
  835. vlan = macvtap_get_vlan(q);
  836. if (!vlan)
  837. return -EINVAL;
  838. if (flags & IFF_ATTACH_QUEUE)
  839. ret = macvtap_enable_queue(vlan->dev, file, q);
  840. else if (flags & IFF_DETACH_QUEUE)
  841. ret = macvtap_disable_queue(q);
  842. else
  843. ret = -EINVAL;
  844. macvtap_put_vlan(vlan);
  845. return ret;
  846. }
  847. static int set_offload(struct macvtap_queue *q, unsigned long arg)
  848. {
  849. struct macvlan_dev *vlan;
  850. netdev_features_t features;
  851. netdev_features_t feature_mask = 0;
  852. vlan = rtnl_dereference(q->vlan);
  853. if (!vlan)
  854. return -ENOLINK;
  855. features = vlan->dev->features;
  856. if (arg & TUN_F_CSUM) {
  857. feature_mask = NETIF_F_HW_CSUM;
  858. if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
  859. if (arg & TUN_F_TSO_ECN)
  860. feature_mask |= NETIF_F_TSO_ECN;
  861. if (arg & TUN_F_TSO4)
  862. feature_mask |= NETIF_F_TSO;
  863. if (arg & TUN_F_TSO6)
  864. feature_mask |= NETIF_F_TSO6;
  865. }
  866. if (arg & TUN_F_UFO)
  867. feature_mask |= NETIF_F_UFO;
  868. }
  869. /* tun/tap driver inverts the usage for TSO offloads, where
  870. * setting the TSO bit means that the userspace wants to
  871. * accept TSO frames and turning it off means that user space
  872. * does not support TSO.
  873. * For macvtap, we have to invert it to mean the same thing.
  874. * When user space turns off TSO, we turn off GSO/LRO so that
  875. * user-space will not receive TSO frames.
  876. */
  877. if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_UFO))
  878. features |= RX_OFFLOADS;
  879. else
  880. features &= ~RX_OFFLOADS;
  881. /* tap_features are the same as features on tun/tap and
  882. * reflect user expectations.
  883. */
  884. vlan->tap_features = vlan->dev->features &
  885. (feature_mask | ~TUN_OFFLOADS);
  886. vlan->set_features = features;
  887. netdev_update_features(vlan->dev);
  888. return 0;
  889. }
  890. /*
  891. * provide compatibility with generic tun/tap interface
  892. */
  893. static long macvtap_ioctl(struct file *file, unsigned int cmd,
  894. unsigned long arg)
  895. {
  896. struct macvtap_queue *q = file->private_data;
  897. struct macvlan_dev *vlan;
  898. void __user *argp = (void __user *)arg;
  899. struct ifreq __user *ifr = argp;
  900. unsigned int __user *up = argp;
  901. unsigned int u;
  902. int __user *sp = argp;
  903. int s;
  904. int ret;
  905. switch (cmd) {
  906. case TUNSETIFF:
  907. /* ignore the name, just look at flags */
  908. if (get_user(u, &ifr->ifr_flags))
  909. return -EFAULT;
  910. ret = 0;
  911. if ((u & ~(IFF_VNET_HDR | IFF_MULTI_QUEUE)) !=
  912. (IFF_NO_PI | IFF_TAP))
  913. ret = -EINVAL;
  914. else
  915. q->flags = u;
  916. return ret;
  917. case TUNGETIFF:
  918. rtnl_lock();
  919. vlan = macvtap_get_vlan(q);
  920. if (!vlan) {
  921. rtnl_unlock();
  922. return -ENOLINK;
  923. }
  924. ret = 0;
  925. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  926. put_user(q->flags, &ifr->ifr_flags))
  927. ret = -EFAULT;
  928. macvtap_put_vlan(vlan);
  929. rtnl_unlock();
  930. return ret;
  931. case TUNSETQUEUE:
  932. if (get_user(u, &ifr->ifr_flags))
  933. return -EFAULT;
  934. rtnl_lock();
  935. ret = macvtap_ioctl_set_queue(file, u);
  936. rtnl_unlock();
  937. case TUNGETFEATURES:
  938. if (put_user(IFF_TAP | IFF_NO_PI | IFF_VNET_HDR |
  939. IFF_MULTI_QUEUE, up))
  940. return -EFAULT;
  941. return 0;
  942. case TUNSETSNDBUF:
  943. if (get_user(u, up))
  944. return -EFAULT;
  945. q->sk.sk_sndbuf = u;
  946. return 0;
  947. case TUNGETVNETHDRSZ:
  948. s = q->vnet_hdr_sz;
  949. if (put_user(s, sp))
  950. return -EFAULT;
  951. return 0;
  952. case TUNSETVNETHDRSZ:
  953. if (get_user(s, sp))
  954. return -EFAULT;
  955. if (s < (int)sizeof(struct virtio_net_hdr))
  956. return -EINVAL;
  957. q->vnet_hdr_sz = s;
  958. return 0;
  959. case TUNSETOFFLOAD:
  960. /* let the user check for future flags */
  961. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  962. TUN_F_TSO_ECN | TUN_F_UFO))
  963. return -EINVAL;
  964. /* TODO: only accept frames with the features that
  965. got enabled for forwarded frames */
  966. if (!(q->flags & IFF_VNET_HDR))
  967. return -EINVAL;
  968. rtnl_lock();
  969. ret = set_offload(q, arg);
  970. rtnl_unlock();
  971. return ret;
  972. default:
  973. return -EINVAL;
  974. }
  975. }
  976. #ifdef CONFIG_COMPAT
  977. static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
  978. unsigned long arg)
  979. {
  980. return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  981. }
  982. #endif
  983. static const struct file_operations macvtap_fops = {
  984. .owner = THIS_MODULE,
  985. .open = macvtap_open,
  986. .release = macvtap_release,
  987. .aio_read = macvtap_aio_read,
  988. .aio_write = macvtap_aio_write,
  989. .poll = macvtap_poll,
  990. .llseek = no_llseek,
  991. .unlocked_ioctl = macvtap_ioctl,
  992. #ifdef CONFIG_COMPAT
  993. .compat_ioctl = macvtap_compat_ioctl,
  994. #endif
  995. };
  996. static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
  997. struct msghdr *m, size_t total_len)
  998. {
  999. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  1000. return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen,
  1001. m->msg_flags & MSG_DONTWAIT);
  1002. }
  1003. static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
  1004. struct msghdr *m, size_t total_len,
  1005. int flags)
  1006. {
  1007. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  1008. int ret;
  1009. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  1010. return -EINVAL;
  1011. ret = macvtap_do_read(q, iocb, m->msg_iov, total_len,
  1012. flags & MSG_DONTWAIT);
  1013. if (ret > total_len) {
  1014. m->msg_flags |= MSG_TRUNC;
  1015. ret = flags & MSG_TRUNC ? ret : total_len;
  1016. }
  1017. return ret;
  1018. }
  1019. /* Ops structure to mimic raw sockets with tun */
  1020. static const struct proto_ops macvtap_socket_ops = {
  1021. .sendmsg = macvtap_sendmsg,
  1022. .recvmsg = macvtap_recvmsg,
  1023. };
  1024. /* Get an underlying socket object from tun file. Returns error unless file is
  1025. * attached to a device. The returned object works like a packet socket, it
  1026. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1027. * holding a reference to the file for as long as the socket is in use. */
  1028. struct socket *macvtap_get_socket(struct file *file)
  1029. {
  1030. struct macvtap_queue *q;
  1031. if (file->f_op != &macvtap_fops)
  1032. return ERR_PTR(-EINVAL);
  1033. q = file->private_data;
  1034. if (!q)
  1035. return ERR_PTR(-EBADFD);
  1036. return &q->sock;
  1037. }
  1038. EXPORT_SYMBOL_GPL(macvtap_get_socket);
  1039. static int macvtap_device_event(struct notifier_block *unused,
  1040. unsigned long event, void *ptr)
  1041. {
  1042. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1043. struct macvlan_dev *vlan;
  1044. struct device *classdev;
  1045. dev_t devt;
  1046. int err;
  1047. if (dev->rtnl_link_ops != &macvtap_link_ops)
  1048. return NOTIFY_DONE;
  1049. vlan = netdev_priv(dev);
  1050. switch (event) {
  1051. case NETDEV_REGISTER:
  1052. /* Create the device node here after the network device has
  1053. * been registered but before register_netdevice has
  1054. * finished running.
  1055. */
  1056. err = macvtap_get_minor(vlan);
  1057. if (err)
  1058. return notifier_from_errno(err);
  1059. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  1060. classdev = device_create(macvtap_class, &dev->dev, devt,
  1061. dev, "tap%d", dev->ifindex);
  1062. if (IS_ERR(classdev)) {
  1063. macvtap_free_minor(vlan);
  1064. return notifier_from_errno(PTR_ERR(classdev));
  1065. }
  1066. break;
  1067. case NETDEV_UNREGISTER:
  1068. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  1069. device_destroy(macvtap_class, devt);
  1070. macvtap_free_minor(vlan);
  1071. break;
  1072. }
  1073. return NOTIFY_DONE;
  1074. }
  1075. static struct notifier_block macvtap_notifier_block __read_mostly = {
  1076. .notifier_call = macvtap_device_event,
  1077. };
  1078. static int macvtap_init(void)
  1079. {
  1080. int err;
  1081. err = alloc_chrdev_region(&macvtap_major, 0,
  1082. MACVTAP_NUM_DEVS, "macvtap");
  1083. if (err)
  1084. goto out1;
  1085. cdev_init(&macvtap_cdev, &macvtap_fops);
  1086. err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
  1087. if (err)
  1088. goto out2;
  1089. macvtap_class = class_create(THIS_MODULE, "macvtap");
  1090. if (IS_ERR(macvtap_class)) {
  1091. err = PTR_ERR(macvtap_class);
  1092. goto out3;
  1093. }
  1094. err = register_netdevice_notifier(&macvtap_notifier_block);
  1095. if (err)
  1096. goto out4;
  1097. err = macvlan_link_register(&macvtap_link_ops);
  1098. if (err)
  1099. goto out5;
  1100. return 0;
  1101. out5:
  1102. unregister_netdevice_notifier(&macvtap_notifier_block);
  1103. out4:
  1104. class_unregister(macvtap_class);
  1105. out3:
  1106. cdev_del(&macvtap_cdev);
  1107. out2:
  1108. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1109. out1:
  1110. return err;
  1111. }
  1112. module_init(macvtap_init);
  1113. static void macvtap_exit(void)
  1114. {
  1115. rtnl_link_unregister(&macvtap_link_ops);
  1116. unregister_netdevice_notifier(&macvtap_notifier_block);
  1117. class_unregister(macvtap_class);
  1118. cdev_del(&macvtap_cdev);
  1119. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1120. }
  1121. module_exit(macvtap_exit);
  1122. MODULE_ALIAS_RTNL_LINK("macvtap");
  1123. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  1124. MODULE_LICENSE("GPL");