tap.c 31 KB

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