tap.c 31 KB

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