PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/net/core/datagram.c

https://github.com/kvaneesh/linux
C | 837 lines | 528 code | 104 blank | 205 comment | 118 complexity | 3ff54ea123ce5ab90a8de00cf8962b0a MD5 | raw file
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SUCS NET3:
  4. *
  5. * Generic datagram handling routines. These are generic for all
  6. * protocols. Possibly a generic IP version on top of these would
  7. * make sense. Not tonight however 8-).
  8. * This is used because UDP, RAW, PACKET, DDP, IPX, AX.25 and
  9. * NetROM layer all have identical poll code and mostly
  10. * identical recvmsg() code. So we share it here. The poll was
  11. * shared before but buried in udp.c so I moved it.
  12. *
  13. * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old
  14. * udp.c code)
  15. *
  16. * Fixes:
  17. * Alan Cox : NULL return from skb_peek_copy()
  18. * understood
  19. * Alan Cox : Rewrote skb_read_datagram to avoid the
  20. * skb_peek_copy stuff.
  21. * Alan Cox : Added support for SOCK_SEQPACKET.
  22. * IPX can no longer use the SO_TYPE hack
  23. * but AX.25 now works right, and SPX is
  24. * feasible.
  25. * Alan Cox : Fixed write poll of non IP protocol
  26. * crash.
  27. * Florian La Roche: Changed for my new skbuff handling.
  28. * Darryl Miles : Fixed non-blocking SOCK_SEQPACKET.
  29. * Linus Torvalds : BSD semantic fixes.
  30. * Alan Cox : Datagram iovec handling
  31. * Darryl Miles : Fixed non-blocking SOCK_STREAM.
  32. * Alan Cox : POSIXisms
  33. * Pete Wyckoff : Unconnected accept() fix.
  34. *
  35. */
  36. #include <linux/module.h>
  37. #include <linux/types.h>
  38. #include <linux/kernel.h>
  39. #include <linux/uaccess.h>
  40. #include <linux/mm.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/errno.h>
  43. #include <linux/sched.h>
  44. #include <linux/inet.h>
  45. #include <linux/netdevice.h>
  46. #include <linux/rtnetlink.h>
  47. #include <linux/poll.h>
  48. #include <linux/highmem.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/slab.h>
  51. #include <linux/pagemap.h>
  52. #include <linux/uio.h>
  53. #include <linux/indirect_call_wrapper.h>
  54. #include <net/protocol.h>
  55. #include <linux/skbuff.h>
  56. #include <net/checksum.h>
  57. #include <net/sock.h>
  58. #include <net/tcp_states.h>
  59. #include <trace/events/skb.h>
  60. #include <net/busy_poll.h>
  61. #include "datagram.h"
  62. /*
  63. * Is a socket 'connection oriented' ?
  64. */
  65. static inline int connection_based(struct sock *sk)
  66. {
  67. return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM;
  68. }
  69. static int receiver_wake_function(wait_queue_entry_t *wait, unsigned int mode, int sync,
  70. void *key)
  71. {
  72. /*
  73. * Avoid a wakeup if event not interesting for us
  74. */
  75. if (key && !(key_to_poll(key) & (EPOLLIN | EPOLLERR)))
  76. return 0;
  77. return autoremove_wake_function(wait, mode, sync, key);
  78. }
  79. /*
  80. * Wait for the last received packet to be different from skb
  81. */
  82. int __skb_wait_for_more_packets(struct sock *sk, struct sk_buff_head *queue,
  83. int *err, long *timeo_p,
  84. const struct sk_buff *skb)
  85. {
  86. int error;
  87. DEFINE_WAIT_FUNC(wait, receiver_wake_function);
  88. prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  89. /* Socket errors? */
  90. error = sock_error(sk);
  91. if (error)
  92. goto out_err;
  93. if (READ_ONCE(queue->prev) != skb)
  94. goto out;
  95. /* Socket shut down? */
  96. if (sk->sk_shutdown & RCV_SHUTDOWN)
  97. goto out_noerr;
  98. /* Sequenced packets can come disconnected.
  99. * If so we report the problem
  100. */
  101. error = -ENOTCONN;
  102. if (connection_based(sk) &&
  103. !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN))
  104. goto out_err;
  105. /* handle signals */
  106. if (signal_pending(current))
  107. goto interrupted;
  108. error = 0;
  109. *timeo_p = schedule_timeout(*timeo_p);
  110. out:
  111. finish_wait(sk_sleep(sk), &wait);
  112. return error;
  113. interrupted:
  114. error = sock_intr_errno(*timeo_p);
  115. out_err:
  116. *err = error;
  117. goto out;
  118. out_noerr:
  119. *err = 0;
  120. error = 1;
  121. goto out;
  122. }
  123. EXPORT_SYMBOL(__skb_wait_for_more_packets);
  124. static struct sk_buff *skb_set_peeked(struct sk_buff *skb)
  125. {
  126. struct sk_buff *nskb;
  127. if (skb->peeked)
  128. return skb;
  129. /* We have to unshare an skb before modifying it. */
  130. if (!skb_shared(skb))
  131. goto done;
  132. nskb = skb_clone(skb, GFP_ATOMIC);
  133. if (!nskb)
  134. return ERR_PTR(-ENOMEM);
  135. skb->prev->next = nskb;
  136. skb->next->prev = nskb;
  137. nskb->prev = skb->prev;
  138. nskb->next = skb->next;
  139. consume_skb(skb);
  140. skb = nskb;
  141. done:
  142. skb->peeked = 1;
  143. return skb;
  144. }
  145. struct sk_buff *__skb_try_recv_from_queue(struct sock *sk,
  146. struct sk_buff_head *queue,
  147. unsigned int flags,
  148. int *off, int *err,
  149. struct sk_buff **last)
  150. {
  151. bool peek_at_off = false;
  152. struct sk_buff *skb;
  153. int _off = 0;
  154. if (unlikely(flags & MSG_PEEK && *off >= 0)) {
  155. peek_at_off = true;
  156. _off = *off;
  157. }
  158. *last = queue->prev;
  159. skb_queue_walk(queue, skb) {
  160. if (flags & MSG_PEEK) {
  161. if (peek_at_off && _off >= skb->len &&
  162. (_off || skb->peeked)) {
  163. _off -= skb->len;
  164. continue;
  165. }
  166. if (!skb->len) {
  167. skb = skb_set_peeked(skb);
  168. if (IS_ERR(skb)) {
  169. *err = PTR_ERR(skb);
  170. return NULL;
  171. }
  172. }
  173. refcount_inc(&skb->users);
  174. } else {
  175. __skb_unlink(skb, queue);
  176. }
  177. *off = _off;
  178. return skb;
  179. }
  180. return NULL;
  181. }
  182. /**
  183. * __skb_try_recv_datagram - Receive a datagram skbuff
  184. * @sk: socket
  185. * @queue: socket queue from which to receive
  186. * @flags: MSG\_ flags
  187. * @off: an offset in bytes to peek skb from. Returns an offset
  188. * within an skb where data actually starts
  189. * @err: error code returned
  190. * @last: set to last peeked message to inform the wait function
  191. * what to look for when peeking
  192. *
  193. * Get a datagram skbuff, understands the peeking, nonblocking wakeups
  194. * and possible races. This replaces identical code in packet, raw and
  195. * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
  196. * the long standing peek and read race for datagram sockets. If you
  197. * alter this routine remember it must be re-entrant.
  198. *
  199. * This function will lock the socket if a skb is returned, so
  200. * the caller needs to unlock the socket in that case (usually by
  201. * calling skb_free_datagram). Returns NULL with @err set to
  202. * -EAGAIN if no data was available or to some other value if an
  203. * error was detected.
  204. *
  205. * * It does not lock socket since today. This function is
  206. * * free of race conditions. This measure should/can improve
  207. * * significantly datagram socket latencies at high loads,
  208. * * when data copying to user space takes lots of time.
  209. * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
  210. * * 8) Great win.)
  211. * * --ANK (980729)
  212. *
  213. * The order of the tests when we find no data waiting are specified
  214. * quite explicitly by POSIX 1003.1g, don't change them without having
  215. * the standard around please.
  216. */
  217. struct sk_buff *__skb_try_recv_datagram(struct sock *sk,
  218. struct sk_buff_head *queue,
  219. unsigned int flags, int *off, int *err,
  220. struct sk_buff **last)
  221. {
  222. struct sk_buff *skb;
  223. unsigned long cpu_flags;
  224. /*
  225. * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
  226. */
  227. int error = sock_error(sk);
  228. if (error)
  229. goto no_packet;
  230. do {
  231. /* Again only user level code calls this function, so nothing
  232. * interrupt level will suddenly eat the receive_queue.
  233. *
  234. * Look at current nfs client by the way...
  235. * However, this function was correct in any case. 8)
  236. */
  237. spin_lock_irqsave(&queue->lock, cpu_flags);
  238. skb = __skb_try_recv_from_queue(sk, queue, flags, off, &error,
  239. last);
  240. spin_unlock_irqrestore(&queue->lock, cpu_flags);
  241. if (error)
  242. goto no_packet;
  243. if (skb)
  244. return skb;
  245. if (!sk_can_busy_loop(sk))
  246. break;
  247. sk_busy_loop(sk, flags & MSG_DONTWAIT);
  248. } while (READ_ONCE(queue->prev) != *last);
  249. error = -EAGAIN;
  250. no_packet:
  251. *err = error;
  252. return NULL;
  253. }
  254. EXPORT_SYMBOL(__skb_try_recv_datagram);
  255. struct sk_buff *__skb_recv_datagram(struct sock *sk,
  256. struct sk_buff_head *sk_queue,
  257. unsigned int flags, int *off, int *err)
  258. {
  259. struct sk_buff *skb, *last;
  260. long timeo;
  261. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  262. do {
  263. skb = __skb_try_recv_datagram(sk, sk_queue, flags, off, err,
  264. &last);
  265. if (skb)
  266. return skb;
  267. if (*err != -EAGAIN)
  268. break;
  269. } while (timeo &&
  270. !__skb_wait_for_more_packets(sk, sk_queue, err,
  271. &timeo, last));
  272. return NULL;
  273. }
  274. EXPORT_SYMBOL(__skb_recv_datagram);
  275. struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
  276. int noblock, int *err)
  277. {
  278. int off = 0;
  279. return __skb_recv_datagram(sk, &sk->sk_receive_queue,
  280. flags | (noblock ? MSG_DONTWAIT : 0),
  281. &off, err);
  282. }
  283. EXPORT_SYMBOL(skb_recv_datagram);
  284. void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
  285. {
  286. consume_skb(skb);
  287. sk_mem_reclaim_partial(sk);
  288. }
  289. EXPORT_SYMBOL(skb_free_datagram);
  290. void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len)
  291. {
  292. bool slow;
  293. if (!skb_unref(skb)) {
  294. sk_peek_offset_bwd(sk, len);
  295. return;
  296. }
  297. slow = lock_sock_fast(sk);
  298. sk_peek_offset_bwd(sk, len);
  299. skb_orphan(skb);
  300. sk_mem_reclaim_partial(sk);
  301. unlock_sock_fast(sk, slow);
  302. /* skb is now orphaned, can be freed outside of locked section */
  303. __kfree_skb(skb);
  304. }
  305. EXPORT_SYMBOL(__skb_free_datagram_locked);
  306. int __sk_queue_drop_skb(struct sock *sk, struct sk_buff_head *sk_queue,
  307. struct sk_buff *skb, unsigned int flags,
  308. void (*destructor)(struct sock *sk,
  309. struct sk_buff *skb))
  310. {
  311. int err = 0;
  312. if (flags & MSG_PEEK) {
  313. err = -ENOENT;
  314. spin_lock_bh(&sk_queue->lock);
  315. if (skb->next) {
  316. __skb_unlink(skb, sk_queue);
  317. refcount_dec(&skb->users);
  318. if (destructor)
  319. destructor(sk, skb);
  320. err = 0;
  321. }
  322. spin_unlock_bh(&sk_queue->lock);
  323. }
  324. atomic_inc(&sk->sk_drops);
  325. return err;
  326. }
  327. EXPORT_SYMBOL(__sk_queue_drop_skb);
  328. /**
  329. * skb_kill_datagram - Free a datagram skbuff forcibly
  330. * @sk: socket
  331. * @skb: datagram skbuff
  332. * @flags: MSG\_ flags
  333. *
  334. * This function frees a datagram skbuff that was received by
  335. * skb_recv_datagram. The flags argument must match the one
  336. * used for skb_recv_datagram.
  337. *
  338. * If the MSG_PEEK flag is set, and the packet is still on the
  339. * receive queue of the socket, it will be taken off the queue
  340. * before it is freed.
  341. *
  342. * This function currently only disables BH when acquiring the
  343. * sk_receive_queue lock. Therefore it must not be used in a
  344. * context where that lock is acquired in an IRQ context.
  345. *
  346. * It returns 0 if the packet was removed by us.
  347. */
  348. int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
  349. {
  350. int err = __sk_queue_drop_skb(sk, &sk->sk_receive_queue, skb, flags,
  351. NULL);
  352. kfree_skb(skb);
  353. sk_mem_reclaim_partial(sk);
  354. return err;
  355. }
  356. EXPORT_SYMBOL(skb_kill_datagram);
  357. INDIRECT_CALLABLE_DECLARE(static size_t simple_copy_to_iter(const void *addr,
  358. size_t bytes,
  359. void *data __always_unused,
  360. struct iov_iter *i));
  361. static int __skb_datagram_iter(const struct sk_buff *skb, int offset,
  362. struct iov_iter *to, int len, bool fault_short,
  363. size_t (*cb)(const void *, size_t, void *,
  364. struct iov_iter *), void *data)
  365. {
  366. int start = skb_headlen(skb);
  367. int i, copy = start - offset, start_off = offset, n;
  368. struct sk_buff *frag_iter;
  369. /* Copy header. */
  370. if (copy > 0) {
  371. if (copy > len)
  372. copy = len;
  373. n = INDIRECT_CALL_1(cb, simple_copy_to_iter,
  374. skb->data + offset, copy, data, to);
  375. offset += n;
  376. if (n != copy)
  377. goto short_copy;
  378. if ((len -= copy) == 0)
  379. return 0;
  380. }
  381. /* Copy paged appendix. Hmm... why does this look so complicated? */
  382. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  383. int end;
  384. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  385. WARN_ON(start > offset + len);
  386. end = start + skb_frag_size(frag);
  387. if ((copy = end - offset) > 0) {
  388. struct page *page = skb_frag_page(frag);
  389. u8 *vaddr = kmap(page);
  390. if (copy > len)
  391. copy = len;
  392. n = INDIRECT_CALL_1(cb, simple_copy_to_iter,
  393. vaddr + skb_frag_off(frag) + offset - start,
  394. copy, data, to);
  395. kunmap(page);
  396. offset += n;
  397. if (n != copy)
  398. goto short_copy;
  399. if (!(len -= copy))
  400. return 0;
  401. }
  402. start = end;
  403. }
  404. skb_walk_frags(skb, frag_iter) {
  405. int end;
  406. WARN_ON(start > offset + len);
  407. end = start + frag_iter->len;
  408. if ((copy = end - offset) > 0) {
  409. if (copy > len)
  410. copy = len;
  411. if (__skb_datagram_iter(frag_iter, offset - start,
  412. to, copy, fault_short, cb, data))
  413. goto fault;
  414. if ((len -= copy) == 0)
  415. return 0;
  416. offset += copy;
  417. }
  418. start = end;
  419. }
  420. if (!len)
  421. return 0;
  422. /* This is not really a user copy fault, but rather someone
  423. * gave us a bogus length on the skb. We should probably
  424. * print a warning here as it may indicate a kernel bug.
  425. */
  426. fault:
  427. iov_iter_revert(to, offset - start_off);
  428. return -EFAULT;
  429. short_copy:
  430. if (fault_short || iov_iter_count(to))
  431. goto fault;
  432. return 0;
  433. }
  434. /**
  435. * skb_copy_and_hash_datagram_iter - Copy datagram to an iovec iterator
  436. * and update a hash.
  437. * @skb: buffer to copy
  438. * @offset: offset in the buffer to start copying from
  439. * @to: iovec iterator to copy to
  440. * @len: amount of data to copy from buffer to iovec
  441. * @hash: hash request to update
  442. */
  443. int skb_copy_and_hash_datagram_iter(const struct sk_buff *skb, int offset,
  444. struct iov_iter *to, int len,
  445. struct ahash_request *hash)
  446. {
  447. return __skb_datagram_iter(skb, offset, to, len, true,
  448. hash_and_copy_to_iter, hash);
  449. }
  450. EXPORT_SYMBOL(skb_copy_and_hash_datagram_iter);
  451. static size_t simple_copy_to_iter(const void *addr, size_t bytes,
  452. void *data __always_unused, struct iov_iter *i)
  453. {
  454. return copy_to_iter(addr, bytes, i);
  455. }
  456. /**
  457. * skb_copy_datagram_iter - Copy a datagram to an iovec iterator.
  458. * @skb: buffer to copy
  459. * @offset: offset in the buffer to start copying from
  460. * @to: iovec iterator to copy to
  461. * @len: amount of data to copy from buffer to iovec
  462. */
  463. int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
  464. struct iov_iter *to, int len)
  465. {
  466. trace_skb_copy_datagram_iovec(skb, len);
  467. return __skb_datagram_iter(skb, offset, to, len, false,
  468. simple_copy_to_iter, NULL);
  469. }
  470. EXPORT_SYMBOL(skb_copy_datagram_iter);
  471. /**
  472. * skb_copy_datagram_from_iter - Copy a datagram from an iov_iter.
  473. * @skb: buffer to copy
  474. * @offset: offset in the buffer to start copying to
  475. * @from: the copy source
  476. * @len: amount of data to copy to buffer from iovec
  477. *
  478. * Returns 0 or -EFAULT.
  479. */
  480. int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
  481. struct iov_iter *from,
  482. int len)
  483. {
  484. int start = skb_headlen(skb);
  485. int i, copy = start - offset;
  486. struct sk_buff *frag_iter;
  487. /* Copy header. */
  488. if (copy > 0) {
  489. if (copy > len)
  490. copy = len;
  491. if (copy_from_iter(skb->data + offset, copy, from) != copy)
  492. goto fault;
  493. if ((len -= copy) == 0)
  494. return 0;
  495. offset += copy;
  496. }
  497. /* Copy paged appendix. Hmm... why does this look so complicated? */
  498. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  499. int end;
  500. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  501. WARN_ON(start > offset + len);
  502. end = start + skb_frag_size(frag);
  503. if ((copy = end - offset) > 0) {
  504. size_t copied;
  505. if (copy > len)
  506. copy = len;
  507. copied = copy_page_from_iter(skb_frag_page(frag),
  508. skb_frag_off(frag) + offset - start,
  509. copy, from);
  510. if (copied != copy)
  511. goto fault;
  512. if (!(len -= copy))
  513. return 0;
  514. offset += copy;
  515. }
  516. start = end;
  517. }
  518. skb_walk_frags(skb, frag_iter) {
  519. int end;
  520. WARN_ON(start > offset + len);
  521. end = start + frag_iter->len;
  522. if ((copy = end - offset) > 0) {
  523. if (copy > len)
  524. copy = len;
  525. if (skb_copy_datagram_from_iter(frag_iter,
  526. offset - start,
  527. from, copy))
  528. goto fault;
  529. if ((len -= copy) == 0)
  530. return 0;
  531. offset += copy;
  532. }
  533. start = end;
  534. }
  535. if (!len)
  536. return 0;
  537. fault:
  538. return -EFAULT;
  539. }
  540. EXPORT_SYMBOL(skb_copy_datagram_from_iter);
  541. int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb,
  542. struct iov_iter *from, size_t length)
  543. {
  544. int frag = skb_shinfo(skb)->nr_frags;
  545. while (length && iov_iter_count(from)) {
  546. struct page *pages[MAX_SKB_FRAGS];
  547. struct page *last_head = NULL;
  548. size_t start;
  549. ssize_t copied;
  550. unsigned long truesize;
  551. int refs, n = 0;
  552. if (frag == MAX_SKB_FRAGS)
  553. return -EMSGSIZE;
  554. copied = iov_iter_get_pages(from, pages, length,
  555. MAX_SKB_FRAGS - frag, &start);
  556. if (copied < 0)
  557. return -EFAULT;
  558. iov_iter_advance(from, copied);
  559. length -= copied;
  560. truesize = PAGE_ALIGN(copied + start);
  561. skb->data_len += copied;
  562. skb->len += copied;
  563. skb->truesize += truesize;
  564. if (sk && sk->sk_type == SOCK_STREAM) {
  565. sk_wmem_queued_add(sk, truesize);
  566. sk_mem_charge(sk, truesize);
  567. } else {
  568. refcount_add(truesize, &skb->sk->sk_wmem_alloc);
  569. }
  570. for (refs = 0; copied != 0; start = 0) {
  571. int size = min_t(int, copied, PAGE_SIZE - start);
  572. struct page *head = compound_head(pages[n]);
  573. start += (pages[n] - head) << PAGE_SHIFT;
  574. copied -= size;
  575. n++;
  576. if (frag) {
  577. skb_frag_t *last = &skb_shinfo(skb)->frags[frag - 1];
  578. if (head == skb_frag_page(last) &&
  579. start == skb_frag_off(last) + skb_frag_size(last)) {
  580. skb_frag_size_add(last, size);
  581. /* We combined this page, we need to release
  582. * a reference. Since compound pages refcount
  583. * is shared among many pages, batch the refcount
  584. * adjustments to limit false sharing.
  585. */
  586. last_head = head;
  587. refs++;
  588. continue;
  589. }
  590. }
  591. if (refs) {
  592. page_ref_sub(last_head, refs);
  593. refs = 0;
  594. }
  595. skb_fill_page_desc(skb, frag++, head, start, size);
  596. }
  597. if (refs)
  598. page_ref_sub(last_head, refs);
  599. }
  600. return 0;
  601. }
  602. EXPORT_SYMBOL(__zerocopy_sg_from_iter);
  603. /**
  604. * zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter
  605. * @skb: buffer to copy
  606. * @from: the source to copy from
  607. *
  608. * The function will first copy up to headlen, and then pin the userspace
  609. * pages and build frags through them.
  610. *
  611. * Returns 0, -EFAULT or -EMSGSIZE.
  612. */
  613. int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from)
  614. {
  615. int copy = min_t(int, skb_headlen(skb), iov_iter_count(from));
  616. /* copy up to skb headlen */
  617. if (skb_copy_datagram_from_iter(skb, 0, from, copy))
  618. return -EFAULT;
  619. return __zerocopy_sg_from_iter(NULL, skb, from, ~0U);
  620. }
  621. EXPORT_SYMBOL(zerocopy_sg_from_iter);
  622. /**
  623. * skb_copy_and_csum_datagram - Copy datagram to an iovec iterator
  624. * and update a checksum.
  625. * @skb: buffer to copy
  626. * @offset: offset in the buffer to start copying from
  627. * @to: iovec iterator to copy to
  628. * @len: amount of data to copy from buffer to iovec
  629. * @csump: checksum pointer
  630. */
  631. static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
  632. struct iov_iter *to, int len,
  633. __wsum *csump)
  634. {
  635. struct csum_state csdata = { .csum = *csump };
  636. int ret;
  637. ret = __skb_datagram_iter(skb, offset, to, len, true,
  638. csum_and_copy_to_iter, &csdata);
  639. if (ret)
  640. return ret;
  641. *csump = csdata.csum;
  642. return 0;
  643. }
  644. /**
  645. * skb_copy_and_csum_datagram_msg - Copy and checksum skb to user iovec.
  646. * @skb: skbuff
  647. * @hlen: hardware length
  648. * @msg: destination
  649. *
  650. * Caller _must_ check that skb will fit to this iovec.
  651. *
  652. * Returns: 0 - success.
  653. * -EINVAL - checksum failure.
  654. * -EFAULT - fault during copy.
  655. */
  656. int skb_copy_and_csum_datagram_msg(struct sk_buff *skb,
  657. int hlen, struct msghdr *msg)
  658. {
  659. __wsum csum;
  660. int chunk = skb->len - hlen;
  661. if (!chunk)
  662. return 0;
  663. if (msg_data_left(msg) < chunk) {
  664. if (__skb_checksum_complete(skb))
  665. return -EINVAL;
  666. if (skb_copy_datagram_msg(skb, hlen, msg, chunk))
  667. goto fault;
  668. } else {
  669. csum = csum_partial(skb->data, hlen, skb->csum);
  670. if (skb_copy_and_csum_datagram(skb, hlen, &msg->msg_iter,
  671. chunk, &csum))
  672. goto fault;
  673. if (csum_fold(csum)) {
  674. iov_iter_revert(&msg->msg_iter, chunk);
  675. return -EINVAL;
  676. }
  677. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
  678. !skb->csum_complete_sw)
  679. netdev_rx_csum_fault(NULL, skb);
  680. }
  681. return 0;
  682. fault:
  683. return -EFAULT;
  684. }
  685. EXPORT_SYMBOL(skb_copy_and_csum_datagram_msg);
  686. /**
  687. * datagram_poll - generic datagram poll
  688. * @file: file struct
  689. * @sock: socket
  690. * @wait: poll table
  691. *
  692. * Datagram poll: Again totally generic. This also handles
  693. * sequenced packet sockets providing the socket receive queue
  694. * is only ever holding data ready to receive.
  695. *
  696. * Note: when you *don't* use this routine for this protocol,
  697. * and you use a different write policy from sock_writeable()
  698. * then please supply your own write_space callback.
  699. */
  700. __poll_t datagram_poll(struct file *file, struct socket *sock,
  701. poll_table *wait)
  702. {
  703. struct sock *sk = sock->sk;
  704. __poll_t mask;
  705. sock_poll_wait(file, sock, wait);
  706. mask = 0;
  707. /* exceptional events? */
  708. if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
  709. mask |= EPOLLERR |
  710. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
  711. if (sk->sk_shutdown & RCV_SHUTDOWN)
  712. mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
  713. if (sk->sk_shutdown == SHUTDOWN_MASK)
  714. mask |= EPOLLHUP;
  715. /* readable? */
  716. if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
  717. mask |= EPOLLIN | EPOLLRDNORM;
  718. /* Connection-based need to check for termination and startup */
  719. if (connection_based(sk)) {
  720. if (sk->sk_state == TCP_CLOSE)
  721. mask |= EPOLLHUP;
  722. /* connection hasn't started yet? */
  723. if (sk->sk_state == TCP_SYN_SENT)
  724. return mask;
  725. }
  726. /* writable? */
  727. if (sock_writeable(sk))
  728. mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
  729. else
  730. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  731. return mask;
  732. }
  733. EXPORT_SYMBOL(datagram_poll);