PageRenderTime 29ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/net/core/datagram.c

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