tap.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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. tap = tap_dev_get_rcu(dev);
  267. if (!tap)
  268. return RX_HANDLER_PASS;
  269. q = tap_get_queue(tap, skb);
  270. if (!q)
  271. return RX_HANDLER_PASS;
  272. skb_push(skb, ETH_HLEN);
  273. /* Apply the forward feature mask so that we perform segmentation
  274. * according to users wishes. This only works if VNET_HDR is
  275. * enabled.
  276. */
  277. if (q->flags & IFF_VNET_HDR)
  278. features |= tap->tap_features;
  279. if (netif_needs_gso(skb, features)) {
  280. struct sk_buff *segs = __skb_gso_segment(skb, features, false);
  281. if (IS_ERR(segs))
  282. goto drop;
  283. if (!segs) {
  284. if (ptr_ring_produce(&q->ring, skb))
  285. goto drop;
  286. goto wake_up;
  287. }
  288. consume_skb(skb);
  289. while (segs) {
  290. struct sk_buff *nskb = segs->next;
  291. segs->next = NULL;
  292. if (ptr_ring_produce(&q->ring, segs)) {
  293. kfree_skb(segs);
  294. kfree_skb_list(nskb);
  295. break;
  296. }
  297. segs = nskb;
  298. }
  299. } else {
  300. /* If we receive a partial checksum and the tap side
  301. * doesn't support checksum offload, compute the checksum.
  302. * Note: it doesn't matter which checksum feature to
  303. * check, we either support them all or none.
  304. */
  305. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  306. !(features & NETIF_F_CSUM_MASK) &&
  307. skb_checksum_help(skb))
  308. goto drop;
  309. if (ptr_ring_produce(&q->ring, skb))
  310. goto drop;
  311. }
  312. wake_up:
  313. wake_up_interruptible_poll(sk_sleep(&q->sk), EPOLLIN | EPOLLRDNORM | EPOLLRDBAND);
  314. return RX_HANDLER_CONSUMED;
  315. drop:
  316. /* Count errors/drops only here, thus don't care about args. */
  317. if (tap->count_rx_dropped)
  318. tap->count_rx_dropped(tap);
  319. kfree_skb(skb);
  320. return RX_HANDLER_CONSUMED;
  321. }
  322. EXPORT_SYMBOL_GPL(tap_handle_frame);
  323. static struct major_info *tap_get_major(int major)
  324. {
  325. struct major_info *tap_major;
  326. list_for_each_entry_rcu(tap_major, &major_list, next) {
  327. if (tap_major->major == major)
  328. return tap_major;
  329. }
  330. return NULL;
  331. }
  332. int tap_get_minor(dev_t major, struct tap_dev *tap)
  333. {
  334. int retval = -ENOMEM;
  335. struct major_info *tap_major;
  336. rcu_read_lock();
  337. tap_major = tap_get_major(MAJOR(major));
  338. if (!tap_major) {
  339. retval = -EINVAL;
  340. goto unlock;
  341. }
  342. spin_lock(&tap_major->minor_lock);
  343. retval = idr_alloc(&tap_major->minor_idr, tap, 1, TAP_NUM_DEVS, GFP_ATOMIC);
  344. if (retval >= 0) {
  345. tap->minor = retval;
  346. } else if (retval == -ENOSPC) {
  347. netdev_err(tap->dev, "Too many tap devices\n");
  348. retval = -EINVAL;
  349. }
  350. spin_unlock(&tap_major->minor_lock);
  351. unlock:
  352. rcu_read_unlock();
  353. return retval < 0 ? retval : 0;
  354. }
  355. EXPORT_SYMBOL_GPL(tap_get_minor);
  356. void tap_free_minor(dev_t major, struct tap_dev *tap)
  357. {
  358. struct major_info *tap_major;
  359. rcu_read_lock();
  360. tap_major = tap_get_major(MAJOR(major));
  361. if (!tap_major) {
  362. goto unlock;
  363. }
  364. spin_lock(&tap_major->minor_lock);
  365. if (tap->minor) {
  366. idr_remove(&tap_major->minor_idr, tap->minor);
  367. tap->minor = 0;
  368. }
  369. spin_unlock(&tap_major->minor_lock);
  370. unlock:
  371. rcu_read_unlock();
  372. }
  373. EXPORT_SYMBOL_GPL(tap_free_minor);
  374. static struct tap_dev *dev_get_by_tap_file(int major, int minor)
  375. {
  376. struct net_device *dev = NULL;
  377. struct tap_dev *tap;
  378. struct major_info *tap_major;
  379. rcu_read_lock();
  380. tap_major = tap_get_major(major);
  381. if (!tap_major) {
  382. tap = NULL;
  383. goto unlock;
  384. }
  385. spin_lock(&tap_major->minor_lock);
  386. tap = idr_find(&tap_major->minor_idr, minor);
  387. if (tap) {
  388. dev = tap->dev;
  389. dev_hold(dev);
  390. }
  391. spin_unlock(&tap_major->minor_lock);
  392. unlock:
  393. rcu_read_unlock();
  394. return tap;
  395. }
  396. static void tap_sock_write_space(struct sock *sk)
  397. {
  398. wait_queue_head_t *wqueue;
  399. if (!sock_writeable(sk) ||
  400. !test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
  401. return;
  402. wqueue = sk_sleep(sk);
  403. if (wqueue && waitqueue_active(wqueue))
  404. wake_up_interruptible_poll(wqueue, EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND);
  405. }
  406. static void tap_sock_destruct(struct sock *sk)
  407. {
  408. struct tap_queue *q = container_of(sk, struct tap_queue, sk);
  409. ptr_ring_cleanup(&q->ring, __skb_array_destroy_skb);
  410. }
  411. static int tap_open(struct inode *inode, struct file *file)
  412. {
  413. struct net *net = current->nsproxy->net_ns;
  414. struct tap_dev *tap;
  415. struct tap_queue *q;
  416. int err = -ENODEV;
  417. rtnl_lock();
  418. tap = dev_get_by_tap_file(imajor(inode), iminor(inode));
  419. if (!tap)
  420. goto err;
  421. err = -ENOMEM;
  422. q = (struct tap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  423. &tap_proto, 0);
  424. if (!q)
  425. goto err;
  426. if (ptr_ring_init(&q->ring, tap->dev->tx_queue_len, GFP_KERNEL)) {
  427. sk_free(&q->sk);
  428. goto err;
  429. }
  430. init_waitqueue_head(&q->sock.wq.wait);
  431. q->sock.type = SOCK_RAW;
  432. q->sock.state = SS_CONNECTED;
  433. q->sock.file = file;
  434. q->sock.ops = &tap_socket_ops;
  435. sock_init_data(&q->sock, &q->sk);
  436. q->sk.sk_write_space = tap_sock_write_space;
  437. q->sk.sk_destruct = tap_sock_destruct;
  438. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  439. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  440. /*
  441. * so far only KVM virtio_net uses tap, enable zero copy between
  442. * guest kernel and host kernel when lower device supports zerocopy
  443. *
  444. * The macvlan supports zerocopy iff the lower device supports zero
  445. * copy so we don't have to look at the lower device directly.
  446. */
  447. if ((tap->dev->features & NETIF_F_HIGHDMA) && (tap->dev->features & NETIF_F_SG))
  448. sock_set_flag(&q->sk, SOCK_ZEROCOPY);
  449. err = tap_set_queue(tap, file, q);
  450. if (err) {
  451. /* tap_sock_destruct() will take care of freeing ptr_ring */
  452. goto err_put;
  453. }
  454. dev_put(tap->dev);
  455. rtnl_unlock();
  456. return err;
  457. err_put:
  458. sock_put(&q->sk);
  459. err:
  460. if (tap)
  461. dev_put(tap->dev);
  462. rtnl_unlock();
  463. return err;
  464. }
  465. static int tap_release(struct inode *inode, struct file *file)
  466. {
  467. struct tap_queue *q = file->private_data;
  468. tap_put_queue(q);
  469. return 0;
  470. }
  471. static __poll_t tap_poll(struct file *file, poll_table *wait)
  472. {
  473. struct tap_queue *q = file->private_data;
  474. __poll_t mask = EPOLLERR;
  475. if (!q)
  476. goto out;
  477. mask = 0;
  478. poll_wait(file, &q->sock.wq.wait, wait);
  479. if (!ptr_ring_empty(&q->ring))
  480. mask |= EPOLLIN | EPOLLRDNORM;
  481. if (sock_writeable(&q->sk) ||
  482. (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) &&
  483. sock_writeable(&q->sk)))
  484. mask |= EPOLLOUT | EPOLLWRNORM;
  485. out:
  486. return mask;
  487. }
  488. static inline struct sk_buff *tap_alloc_skb(struct sock *sk, size_t prepad,
  489. size_t len, size_t linear,
  490. int noblock, int *err)
  491. {
  492. struct sk_buff *skb;
  493. /* Under a page? Don't bother with paged skb. */
  494. if (prepad + len < PAGE_SIZE || !linear)
  495. linear = len;
  496. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  497. err, 0);
  498. if (!skb)
  499. return NULL;
  500. skb_reserve(skb, prepad);
  501. skb_put(skb, linear);
  502. skb->data_len = len - linear;
  503. skb->len += len - linear;
  504. return skb;
  505. }
  506. /* Neighbour code has some assumptions on HH_DATA_MOD alignment */
  507. #define TAP_RESERVE HH_DATA_OFF(ETH_HLEN)
  508. /* Get packet from user space buffer */
  509. static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
  510. struct iov_iter *from, int noblock)
  511. {
  512. int good_linear = SKB_MAX_HEAD(TAP_RESERVE);
  513. struct sk_buff *skb;
  514. struct tap_dev *tap;
  515. unsigned long total_len = iov_iter_count(from);
  516. unsigned long len = total_len;
  517. int err;
  518. struct virtio_net_hdr vnet_hdr = { 0 };
  519. int vnet_hdr_len = 0;
  520. int copylen = 0;
  521. int depth;
  522. bool zerocopy = false;
  523. size_t linear;
  524. if (q->flags & IFF_VNET_HDR) {
  525. vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
  526. err = -EINVAL;
  527. if (len < vnet_hdr_len)
  528. goto err;
  529. len -= vnet_hdr_len;
  530. err = -EFAULT;
  531. if (!copy_from_iter_full(&vnet_hdr, sizeof(vnet_hdr), from))
  532. goto err;
  533. iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
  534. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  535. tap16_to_cpu(q, vnet_hdr.csum_start) +
  536. tap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
  537. tap16_to_cpu(q, vnet_hdr.hdr_len))
  538. vnet_hdr.hdr_len = cpu_to_tap16(q,
  539. tap16_to_cpu(q, vnet_hdr.csum_start) +
  540. tap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
  541. err = -EINVAL;
  542. if (tap16_to_cpu(q, vnet_hdr.hdr_len) > len)
  543. goto err;
  544. }
  545. err = -EINVAL;
  546. if (unlikely(len < ETH_HLEN))
  547. goto err;
  548. if (msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
  549. struct iov_iter i;
  550. copylen = vnet_hdr.hdr_len ?
  551. tap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
  552. if (copylen > good_linear)
  553. copylen = good_linear;
  554. else if (copylen < ETH_HLEN)
  555. copylen = ETH_HLEN;
  556. linear = copylen;
  557. i = *from;
  558. iov_iter_advance(&i, copylen);
  559. if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
  560. zerocopy = true;
  561. }
  562. if (!zerocopy) {
  563. copylen = len;
  564. linear = tap16_to_cpu(q, vnet_hdr.hdr_len);
  565. if (linear > good_linear)
  566. linear = good_linear;
  567. else if (linear < ETH_HLEN)
  568. linear = ETH_HLEN;
  569. }
  570. skb = tap_alloc_skb(&q->sk, TAP_RESERVE, copylen,
  571. linear, noblock, &err);
  572. if (!skb)
  573. goto err;
  574. if (zerocopy)
  575. err = zerocopy_sg_from_iter(skb, from);
  576. else
  577. err = skb_copy_datagram_from_iter(skb, 0, from, len);
  578. if (err)
  579. goto err_kfree;
  580. skb_set_network_header(skb, ETH_HLEN);
  581. skb_reset_mac_header(skb);
  582. skb->protocol = eth_hdr(skb)->h_proto;
  583. if (vnet_hdr_len) {
  584. err = virtio_net_hdr_to_skb(skb, &vnet_hdr,
  585. tap_is_little_endian(q));
  586. if (err)
  587. goto err_kfree;
  588. }
  589. skb_probe_transport_header(skb);
  590. /* Move network header to the right position for VLAN tagged packets */
  591. if ((skb->protocol == htons(ETH_P_8021Q) ||
  592. skb->protocol == htons(ETH_P_8021AD)) &&
  593. __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
  594. skb_set_network_header(skb, depth);
  595. rcu_read_lock();
  596. tap = rcu_dereference(q->tap);
  597. /* copy skb_ubuf_info for callback when skb has no error */
  598. if (zerocopy) {
  599. skb_shinfo(skb)->destructor_arg = msg_control;
  600. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  601. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  602. } else if (msg_control) {
  603. struct ubuf_info *uarg = msg_control;
  604. uarg->callback(uarg, false);
  605. }
  606. if (tap) {
  607. skb->dev = tap->dev;
  608. dev_queue_xmit(skb);
  609. } else {
  610. kfree_skb(skb);
  611. }
  612. rcu_read_unlock();
  613. return total_len;
  614. err_kfree:
  615. kfree_skb(skb);
  616. err:
  617. rcu_read_lock();
  618. tap = rcu_dereference(q->tap);
  619. if (tap && tap->count_tx_dropped)
  620. tap->count_tx_dropped(tap);
  621. rcu_read_unlock();
  622. return err;
  623. }
  624. static ssize_t tap_write_iter(struct kiocb *iocb, struct iov_iter *from)
  625. {
  626. struct file *file = iocb->ki_filp;
  627. struct tap_queue *q = file->private_data;
  628. return tap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
  629. }
  630. /* Put packet to the user space buffer */
  631. static ssize_t tap_put_user(struct tap_queue *q,
  632. const struct sk_buff *skb,
  633. struct iov_iter *iter)
  634. {
  635. int ret;
  636. int vnet_hdr_len = 0;
  637. int vlan_offset = 0;
  638. int total;
  639. if (q->flags & IFF_VNET_HDR) {
  640. int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
  641. struct virtio_net_hdr vnet_hdr;
  642. vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
  643. if (iov_iter_count(iter) < vnet_hdr_len)
  644. return -EINVAL;
  645. if (virtio_net_hdr_from_skb(skb, &vnet_hdr,
  646. tap_is_little_endian(q), true,
  647. vlan_hlen))
  648. BUG();
  649. if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
  650. sizeof(vnet_hdr))
  651. return -EFAULT;
  652. iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
  653. }
  654. total = vnet_hdr_len;
  655. total += skb->len;
  656. if (skb_vlan_tag_present(skb)) {
  657. struct {
  658. __be16 h_vlan_proto;
  659. __be16 h_vlan_TCI;
  660. } veth;
  661. veth.h_vlan_proto = skb->vlan_proto;
  662. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  663. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  664. total += VLAN_HLEN;
  665. ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
  666. if (ret || !iov_iter_count(iter))
  667. goto done;
  668. ret = copy_to_iter(&veth, sizeof(veth), iter);
  669. if (ret != sizeof(veth) || !iov_iter_count(iter))
  670. goto done;
  671. }
  672. ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
  673. skb->len - vlan_offset);
  674. done:
  675. return ret ? ret : total;
  676. }
  677. static ssize_t tap_do_read(struct tap_queue *q,
  678. struct iov_iter *to,
  679. int noblock, struct sk_buff *skb)
  680. {
  681. DEFINE_WAIT(wait);
  682. ssize_t ret = 0;
  683. if (!iov_iter_count(to)) {
  684. kfree_skb(skb);
  685. return 0;
  686. }
  687. if (skb)
  688. goto put;
  689. while (1) {
  690. if (!noblock)
  691. prepare_to_wait(sk_sleep(&q->sk), &wait,
  692. TASK_INTERRUPTIBLE);
  693. /* Read frames from the queue */
  694. skb = ptr_ring_consume(&q->ring);
  695. if (skb)
  696. break;
  697. if (noblock) {
  698. ret = -EAGAIN;
  699. break;
  700. }
  701. if (signal_pending(current)) {
  702. ret = -ERESTARTSYS;
  703. break;
  704. }
  705. /* Nothing to read, let's sleep */
  706. schedule();
  707. }
  708. if (!noblock)
  709. finish_wait(sk_sleep(&q->sk), &wait);
  710. put:
  711. if (skb) {
  712. ret = tap_put_user(q, skb, to);
  713. if (unlikely(ret < 0))
  714. kfree_skb(skb);
  715. else
  716. consume_skb(skb);
  717. }
  718. return ret;
  719. }
  720. static ssize_t tap_read_iter(struct kiocb *iocb, struct iov_iter *to)
  721. {
  722. struct file *file = iocb->ki_filp;
  723. struct tap_queue *q = file->private_data;
  724. ssize_t len = iov_iter_count(to), ret;
  725. ret = tap_do_read(q, to, file->f_flags & O_NONBLOCK, NULL);
  726. ret = min_t(ssize_t, ret, len);
  727. if (ret > 0)
  728. iocb->ki_pos = ret;
  729. return ret;
  730. }
  731. static struct tap_dev *tap_get_tap_dev(struct tap_queue *q)
  732. {
  733. struct tap_dev *tap;
  734. ASSERT_RTNL();
  735. tap = rtnl_dereference(q->tap);
  736. if (tap)
  737. dev_hold(tap->dev);
  738. return tap;
  739. }
  740. static void tap_put_tap_dev(struct tap_dev *tap)
  741. {
  742. dev_put(tap->dev);
  743. }
  744. static int tap_ioctl_set_queue(struct file *file, unsigned int flags)
  745. {
  746. struct tap_queue *q = file->private_data;
  747. struct tap_dev *tap;
  748. int ret;
  749. tap = tap_get_tap_dev(q);
  750. if (!tap)
  751. return -EINVAL;
  752. if (flags & IFF_ATTACH_QUEUE)
  753. ret = tap_enable_queue(tap, file, q);
  754. else if (flags & IFF_DETACH_QUEUE)
  755. ret = tap_disable_queue(q);
  756. else
  757. ret = -EINVAL;
  758. tap_put_tap_dev(tap);
  759. return ret;
  760. }
  761. static int set_offload(struct tap_queue *q, unsigned long arg)
  762. {
  763. struct tap_dev *tap;
  764. netdev_features_t features;
  765. netdev_features_t feature_mask = 0;
  766. tap = rtnl_dereference(q->tap);
  767. if (!tap)
  768. return -ENOLINK;
  769. features = tap->dev->features;
  770. if (arg & TUN_F_CSUM) {
  771. feature_mask = NETIF_F_HW_CSUM;
  772. if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
  773. if (arg & TUN_F_TSO_ECN)
  774. feature_mask |= NETIF_F_TSO_ECN;
  775. if (arg & TUN_F_TSO4)
  776. feature_mask |= NETIF_F_TSO;
  777. if (arg & TUN_F_TSO6)
  778. feature_mask |= NETIF_F_TSO6;
  779. }
  780. }
  781. /* tun/tap driver inverts the usage for TSO offloads, where
  782. * setting the TSO bit means that the userspace wants to
  783. * accept TSO frames and turning it off means that user space
  784. * does not support TSO.
  785. * For tap, we have to invert it to mean the same thing.
  786. * When user space turns off TSO, we turn off GSO/LRO so that
  787. * user-space will not receive TSO frames.
  788. */
  789. if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6))
  790. features |= RX_OFFLOADS;
  791. else
  792. features &= ~RX_OFFLOADS;
  793. /* tap_features are the same as features on tun/tap and
  794. * reflect user expectations.
  795. */
  796. tap->tap_features = feature_mask;
  797. if (tap->update_features)
  798. tap->update_features(tap, features);
  799. return 0;
  800. }
  801. /*
  802. * provide compatibility with generic tun/tap interface
  803. */
  804. static long tap_ioctl(struct file *file, unsigned int cmd,
  805. unsigned long arg)
  806. {
  807. struct tap_queue *q = file->private_data;
  808. struct tap_dev *tap;
  809. void __user *argp = (void __user *)arg;
  810. struct ifreq __user *ifr = argp;
  811. unsigned int __user *up = argp;
  812. unsigned short u;
  813. int __user *sp = argp;
  814. struct sockaddr sa;
  815. int s;
  816. int ret;
  817. switch (cmd) {
  818. case TUNSETIFF:
  819. /* ignore the name, just look at flags */
  820. if (get_user(u, &ifr->ifr_flags))
  821. return -EFAULT;
  822. ret = 0;
  823. if ((u & ~TAP_IFFEATURES) != (IFF_NO_PI | IFF_TAP))
  824. ret = -EINVAL;
  825. else
  826. q->flags = (q->flags & ~TAP_IFFEATURES) | u;
  827. return ret;
  828. case TUNGETIFF:
  829. rtnl_lock();
  830. tap = tap_get_tap_dev(q);
  831. if (!tap) {
  832. rtnl_unlock();
  833. return -ENOLINK;
  834. }
  835. ret = 0;
  836. u = q->flags;
  837. if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
  838. put_user(u, &ifr->ifr_flags))
  839. ret = -EFAULT;
  840. tap_put_tap_dev(tap);
  841. rtnl_unlock();
  842. return ret;
  843. case TUNSETQUEUE:
  844. if (get_user(u, &ifr->ifr_flags))
  845. return -EFAULT;
  846. rtnl_lock();
  847. ret = tap_ioctl_set_queue(file, u);
  848. rtnl_unlock();
  849. return ret;
  850. case TUNGETFEATURES:
  851. if (put_user(IFF_TAP | IFF_NO_PI | TAP_IFFEATURES, up))
  852. return -EFAULT;
  853. return 0;
  854. case TUNSETSNDBUF:
  855. if (get_user(s, sp))
  856. return -EFAULT;
  857. if (s <= 0)
  858. return -EINVAL;
  859. q->sk.sk_sndbuf = s;
  860. return 0;
  861. case TUNGETVNETHDRSZ:
  862. s = q->vnet_hdr_sz;
  863. if (put_user(s, sp))
  864. return -EFAULT;
  865. return 0;
  866. case TUNSETVNETHDRSZ:
  867. if (get_user(s, sp))
  868. return -EFAULT;
  869. if (s < (int)sizeof(struct virtio_net_hdr))
  870. return -EINVAL;
  871. q->vnet_hdr_sz = s;
  872. return 0;
  873. case TUNGETVNETLE:
  874. s = !!(q->flags & TAP_VNET_LE);
  875. if (put_user(s, sp))
  876. return -EFAULT;
  877. return 0;
  878. case TUNSETVNETLE:
  879. if (get_user(s, sp))
  880. return -EFAULT;
  881. if (s)
  882. q->flags |= TAP_VNET_LE;
  883. else
  884. q->flags &= ~TAP_VNET_LE;
  885. return 0;
  886. case TUNGETVNETBE:
  887. return tap_get_vnet_be(q, sp);
  888. case TUNSETVNETBE:
  889. return tap_set_vnet_be(q, sp);
  890. case TUNSETOFFLOAD:
  891. /* let the user check for future flags */
  892. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  893. TUN_F_TSO_ECN | TUN_F_UFO))
  894. return -EINVAL;
  895. rtnl_lock();
  896. ret = set_offload(q, arg);
  897. rtnl_unlock();
  898. return ret;
  899. case SIOCGIFHWADDR:
  900. rtnl_lock();
  901. tap = tap_get_tap_dev(q);
  902. if (!tap) {
  903. rtnl_unlock();
  904. return -ENOLINK;
  905. }
  906. ret = 0;
  907. dev_get_mac_address(&sa, dev_net(tap->dev), tap->dev->name);
  908. if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
  909. copy_to_user(&ifr->ifr_hwaddr, &sa, sizeof(sa)))
  910. ret = -EFAULT;
  911. tap_put_tap_dev(tap);
  912. rtnl_unlock();
  913. return ret;
  914. case SIOCSIFHWADDR:
  915. if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
  916. return -EFAULT;
  917. rtnl_lock();
  918. tap = tap_get_tap_dev(q);
  919. if (!tap) {
  920. rtnl_unlock();
  921. return -ENOLINK;
  922. }
  923. ret = dev_set_mac_address_user(tap->dev, &sa, NULL);
  924. tap_put_tap_dev(tap);
  925. rtnl_unlock();
  926. return ret;
  927. default:
  928. return -EINVAL;
  929. }
  930. }
  931. #ifdef CONFIG_COMPAT
  932. static long tap_compat_ioctl(struct file *file, unsigned int cmd,
  933. unsigned long arg)
  934. {
  935. return tap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  936. }
  937. #endif
  938. static const struct file_operations tap_fops = {
  939. .owner = THIS_MODULE,
  940. .open = tap_open,
  941. .release = tap_release,
  942. .read_iter = tap_read_iter,
  943. .write_iter = tap_write_iter,
  944. .poll = tap_poll,
  945. .llseek = no_llseek,
  946. .unlocked_ioctl = tap_ioctl,
  947. #ifdef CONFIG_COMPAT
  948. .compat_ioctl = tap_compat_ioctl,
  949. #endif
  950. };
  951. static int tap_get_user_xdp(struct tap_queue *q, struct xdp_buff *xdp)
  952. {
  953. struct tun_xdp_hdr *hdr = xdp->data_hard_start;
  954. struct virtio_net_hdr *gso = &hdr->gso;
  955. int buflen = hdr->buflen;
  956. int vnet_hdr_len = 0;
  957. struct tap_dev *tap;
  958. struct sk_buff *skb;
  959. int err, depth;
  960. if (q->flags & IFF_VNET_HDR)
  961. vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
  962. skb = build_skb(xdp->data_hard_start, buflen);
  963. if (!skb) {
  964. err = -ENOMEM;
  965. goto err;
  966. }
  967. skb_reserve(skb, xdp->data - xdp->data_hard_start);
  968. skb_put(skb, xdp->data_end - xdp->data);
  969. skb_set_network_header(skb, ETH_HLEN);
  970. skb_reset_mac_header(skb);
  971. skb->protocol = eth_hdr(skb)->h_proto;
  972. if (vnet_hdr_len) {
  973. err = virtio_net_hdr_to_skb(skb, gso, tap_is_little_endian(q));
  974. if (err)
  975. goto err_kfree;
  976. }
  977. /* Move network header to the right position for VLAN tagged packets */
  978. if ((skb->protocol == htons(ETH_P_8021Q) ||
  979. skb->protocol == htons(ETH_P_8021AD)) &&
  980. __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
  981. skb_set_network_header(skb, depth);
  982. rcu_read_lock();
  983. tap = rcu_dereference(q->tap);
  984. if (tap) {
  985. skb->dev = tap->dev;
  986. skb_probe_transport_header(skb);
  987. dev_queue_xmit(skb);
  988. } else {
  989. kfree_skb(skb);
  990. }
  991. rcu_read_unlock();
  992. return 0;
  993. err_kfree:
  994. kfree_skb(skb);
  995. err:
  996. rcu_read_lock();
  997. tap = rcu_dereference(q->tap);
  998. if (tap && tap->count_tx_dropped)
  999. tap->count_tx_dropped(tap);
  1000. rcu_read_unlock();
  1001. return err;
  1002. }
  1003. static int tap_sendmsg(struct socket *sock, struct msghdr *m,
  1004. size_t total_len)
  1005. {
  1006. struct tap_queue *q = container_of(sock, struct tap_queue, sock);
  1007. struct tun_msg_ctl *ctl = m->msg_control;
  1008. struct xdp_buff *xdp;
  1009. int i;
  1010. if (ctl && (ctl->type == TUN_MSG_PTR)) {
  1011. for (i = 0; i < ctl->num; i++) {
  1012. xdp = &((struct xdp_buff *)ctl->ptr)[i];
  1013. tap_get_user_xdp(q, xdp);
  1014. }
  1015. return 0;
  1016. }
  1017. return tap_get_user(q, ctl ? ctl->ptr : NULL, &m->msg_iter,
  1018. m->msg_flags & MSG_DONTWAIT);
  1019. }
  1020. static int tap_recvmsg(struct socket *sock, struct msghdr *m,
  1021. size_t total_len, int flags)
  1022. {
  1023. struct tap_queue *q = container_of(sock, struct tap_queue, sock);
  1024. struct sk_buff *skb = m->msg_control;
  1025. int ret;
  1026. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
  1027. kfree_skb(skb);
  1028. return -EINVAL;
  1029. }
  1030. ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT, skb);
  1031. if (ret > total_len) {
  1032. m->msg_flags |= MSG_TRUNC;
  1033. ret = flags & MSG_TRUNC ? ret : total_len;
  1034. }
  1035. return ret;
  1036. }
  1037. static int tap_peek_len(struct socket *sock)
  1038. {
  1039. struct tap_queue *q = container_of(sock, struct tap_queue,
  1040. sock);
  1041. return PTR_RING_PEEK_CALL(&q->ring, __skb_array_len_with_tag);
  1042. }
  1043. /* Ops structure to mimic raw sockets with tun */
  1044. static const struct proto_ops tap_socket_ops = {
  1045. .sendmsg = tap_sendmsg,
  1046. .recvmsg = tap_recvmsg,
  1047. .peek_len = tap_peek_len,
  1048. };
  1049. /* Get an underlying socket object from tun file. Returns error unless file is
  1050. * attached to a device. The returned object works like a packet socket, it
  1051. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1052. * holding a reference to the file for as long as the socket is in use. */
  1053. struct socket *tap_get_socket(struct file *file)
  1054. {
  1055. struct tap_queue *q;
  1056. if (file->f_op != &tap_fops)
  1057. return ERR_PTR(-EINVAL);
  1058. q = file->private_data;
  1059. if (!q)
  1060. return ERR_PTR(-EBADFD);
  1061. return &q->sock;
  1062. }
  1063. EXPORT_SYMBOL_GPL(tap_get_socket);
  1064. struct ptr_ring *tap_get_ptr_ring(struct file *file)
  1065. {
  1066. struct tap_queue *q;
  1067. if (file->f_op != &tap_fops)
  1068. return ERR_PTR(-EINVAL);
  1069. q = file->private_data;
  1070. if (!q)
  1071. return ERR_PTR(-EBADFD);
  1072. return &q->ring;
  1073. }
  1074. EXPORT_SYMBOL_GPL(tap_get_ptr_ring);
  1075. int tap_queue_resize(struct tap_dev *tap)
  1076. {
  1077. struct net_device *dev = tap->dev;
  1078. struct tap_queue *q;
  1079. struct ptr_ring **rings;
  1080. int n = tap->numqueues;
  1081. int ret, i = 0;
  1082. rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL);
  1083. if (!rings)
  1084. return -ENOMEM;
  1085. list_for_each_entry(q, &tap->queue_list, next)
  1086. rings[i++] = &q->ring;
  1087. ret = ptr_ring_resize_multiple(rings, n,
  1088. dev->tx_queue_len, GFP_KERNEL,
  1089. __skb_array_destroy_skb);
  1090. kfree(rings);
  1091. return ret;
  1092. }
  1093. EXPORT_SYMBOL_GPL(tap_queue_resize);
  1094. static int tap_list_add(dev_t major, const char *device_name)
  1095. {
  1096. struct major_info *tap_major;
  1097. tap_major = kzalloc(sizeof(*tap_major), GFP_ATOMIC);
  1098. if (!tap_major)
  1099. return -ENOMEM;
  1100. tap_major->major = MAJOR(major);
  1101. idr_init(&tap_major->minor_idr);
  1102. spin_lock_init(&tap_major->minor_lock);
  1103. tap_major->device_name = device_name;
  1104. list_add_tail_rcu(&tap_major->next, &major_list);
  1105. return 0;
  1106. }
  1107. int tap_create_cdev(struct cdev *tap_cdev, dev_t *tap_major,
  1108. const char *device_name, struct module *module)
  1109. {
  1110. int err;
  1111. err = alloc_chrdev_region(tap_major, 0, TAP_NUM_DEVS, device_name);
  1112. if (err)
  1113. goto out1;
  1114. cdev_init(tap_cdev, &tap_fops);
  1115. tap_cdev->owner = module;
  1116. err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS);
  1117. if (err)
  1118. goto out2;
  1119. err = tap_list_add(*tap_major, device_name);
  1120. if (err)
  1121. goto out3;
  1122. return 0;
  1123. out3:
  1124. cdev_del(tap_cdev);
  1125. out2:
  1126. unregister_chrdev_region(*tap_major, TAP_NUM_DEVS);
  1127. out1:
  1128. return err;
  1129. }
  1130. EXPORT_SYMBOL_GPL(tap_create_cdev);
  1131. void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev)
  1132. {
  1133. struct major_info *tap_major, *tmp;
  1134. cdev_del(tap_cdev);
  1135. unregister_chrdev_region(major, TAP_NUM_DEVS);
  1136. list_for_each_entry_safe(tap_major, tmp, &major_list, next) {
  1137. if (tap_major->major == MAJOR(major)) {
  1138. idr_destroy(&tap_major->minor_idr);
  1139. list_del_rcu(&tap_major->next);
  1140. kfree_rcu(tap_major, rcu);
  1141. }
  1142. }
  1143. }
  1144. EXPORT_SYMBOL_GPL(tap_destroy_cdev);
  1145. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  1146. MODULE_AUTHOR("Sainath Grandhi <sainath.grandhi@intel.com>");
  1147. MODULE_LICENSE("GPL");