PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/net/core/datagram.c

https://bitbucket.org/alfredchen/linux-gc
C | 834 lines | 544 code | 105 blank | 185 comment | 129 complexity | 0927dad61a371e09acdee251bcc9d02f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.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_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. /**
  143. * __skb_try_recv_datagram - Receive a datagram skbuff
  144. * @sk: socket
  145. * @flags: MSG_ flags
  146. * @destructor: invoked under the receive lock on successful dequeue
  147. * @peeked: returns non-zero if this packet has been seen before
  148. * @off: an offset in bytes to peek skb from. Returns an offset
  149. * within an skb where data actually starts
  150. * @err: error code returned
  151. * @last: set to last peeked message to inform the wait function
  152. * what to look for when peeking
  153. *
  154. * Get a datagram skbuff, understands the peeking, nonblocking wakeups
  155. * and possible races. This replaces identical code in packet, raw and
  156. * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes
  157. * the long standing peek and read race for datagram sockets. If you
  158. * alter this routine remember it must be re-entrant.
  159. *
  160. * This function will lock the socket if a skb is returned, so
  161. * the caller needs to unlock the socket in that case (usually by
  162. * calling skb_free_datagram). Returns NULL with *err set to
  163. * -EAGAIN if no data was available or to some other value if an
  164. * error was detected.
  165. *
  166. * * It does not lock socket since today. This function is
  167. * * free of race conditions. This measure should/can improve
  168. * * significantly datagram socket latencies at high loads,
  169. * * when data copying to user space takes lots of time.
  170. * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet
  171. * * 8) Great win.)
  172. * * --ANK (980729)
  173. *
  174. * The order of the tests when we find no data waiting are specified
  175. * quite explicitly by POSIX 1003.1g, don't change them without having
  176. * the standard around please.
  177. */
  178. struct sk_buff *__skb_try_recv_datagram(struct sock *sk, unsigned int flags,
  179. void (*destructor)(struct sock *sk,
  180. struct sk_buff *skb),
  181. int *peeked, int *off, int *err,
  182. struct sk_buff **last)
  183. {
  184. struct sk_buff_head *queue = &sk->sk_receive_queue;
  185. struct sk_buff *skb;
  186. unsigned long cpu_flags;
  187. /*
  188. * Caller is allowed not to check sk->sk_err before skb_recv_datagram()
  189. */
  190. int error = sock_error(sk);
  191. if (error)
  192. goto no_packet;
  193. *peeked = 0;
  194. do {
  195. /* Again only user level code calls this function, so nothing
  196. * interrupt level will suddenly eat the receive_queue.
  197. *
  198. * Look at current nfs client by the way...
  199. * However, this function was correct in any case. 8)
  200. */
  201. int _off = *off;
  202. *last = (struct sk_buff *)queue;
  203. spin_lock_irqsave(&queue->lock, cpu_flags);
  204. skb_queue_walk(queue, skb) {
  205. *last = skb;
  206. if (flags & MSG_PEEK) {
  207. if (_off >= skb->len && (skb->len || _off ||
  208. skb->peeked)) {
  209. _off -= skb->len;
  210. continue;
  211. }
  212. if (!skb->len) {
  213. skb = skb_set_peeked(skb);
  214. if (IS_ERR(skb)) {
  215. error = PTR_ERR(skb);
  216. spin_unlock_irqrestore(&queue->lock,
  217. cpu_flags);
  218. goto no_packet;
  219. }
  220. }
  221. *peeked = 1;
  222. atomic_inc(&skb->users);
  223. } else {
  224. __skb_unlink(skb, queue);
  225. if (destructor)
  226. destructor(sk, skb);
  227. }
  228. spin_unlock_irqrestore(&queue->lock, cpu_flags);
  229. *off = _off;
  230. return skb;
  231. }
  232. spin_unlock_irqrestore(&queue->lock, cpu_flags);
  233. } while (sk_can_busy_loop(sk) &&
  234. sk_busy_loop(sk, flags & MSG_DONTWAIT));
  235. error = -EAGAIN;
  236. no_packet:
  237. *err = error;
  238. return NULL;
  239. }
  240. EXPORT_SYMBOL(__skb_try_recv_datagram);
  241. struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned int flags,
  242. void (*destructor)(struct sock *sk,
  243. struct sk_buff *skb),
  244. int *peeked, int *off, int *err)
  245. {
  246. struct sk_buff *skb, *last;
  247. long timeo;
  248. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  249. do {
  250. skb = __skb_try_recv_datagram(sk, flags, destructor, peeked,
  251. off, err, &last);
  252. if (skb)
  253. return skb;
  254. if (*err != -EAGAIN)
  255. break;
  256. } while (timeo &&
  257. !__skb_wait_for_more_packets(sk, err, &timeo, last));
  258. return NULL;
  259. }
  260. EXPORT_SYMBOL(__skb_recv_datagram);
  261. struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned int flags,
  262. int noblock, int *err)
  263. {
  264. int peeked, off = 0;
  265. return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
  266. NULL, &peeked, &off, err);
  267. }
  268. EXPORT_SYMBOL(skb_recv_datagram);
  269. void skb_free_datagram(struct sock *sk, struct sk_buff *skb)
  270. {
  271. consume_skb(skb);
  272. sk_mem_reclaim_partial(sk);
  273. }
  274. EXPORT_SYMBOL(skb_free_datagram);
  275. void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len)
  276. {
  277. bool slow;
  278. if (likely(atomic_read(&skb->users) == 1))
  279. smp_rmb();
  280. else if (likely(!atomic_dec_and_test(&skb->users))) {
  281. sk_peek_offset_bwd(sk, len);
  282. return;
  283. }
  284. slow = lock_sock_fast(sk);
  285. sk_peek_offset_bwd(sk, len);
  286. skb_orphan(skb);
  287. sk_mem_reclaim_partial(sk);
  288. unlock_sock_fast(sk, slow);
  289. /* skb is now orphaned, can be freed outside of locked section */
  290. __kfree_skb(skb);
  291. }
  292. EXPORT_SYMBOL(__skb_free_datagram_locked);
  293. int __sk_queue_drop_skb(struct sock *sk, struct sk_buff *skb,
  294. unsigned int flags,
  295. void (*destructor)(struct sock *sk,
  296. struct sk_buff *skb))
  297. {
  298. int err = 0;
  299. if (flags & MSG_PEEK) {
  300. err = -ENOENT;
  301. spin_lock_bh(&sk->sk_receive_queue.lock);
  302. if (skb == skb_peek(&sk->sk_receive_queue)) {
  303. __skb_unlink(skb, &sk->sk_receive_queue);
  304. atomic_dec(&skb->users);
  305. if (destructor)
  306. destructor(sk, skb);
  307. err = 0;
  308. }
  309. spin_unlock_bh(&sk->sk_receive_queue.lock);
  310. }
  311. atomic_inc(&sk->sk_drops);
  312. return err;
  313. }
  314. EXPORT_SYMBOL(__sk_queue_drop_skb);
  315. /**
  316. * skb_kill_datagram - Free a datagram skbuff forcibly
  317. * @sk: socket
  318. * @skb: datagram skbuff
  319. * @flags: MSG_ flags
  320. *
  321. * This function frees a datagram skbuff that was received by
  322. * skb_recv_datagram. The flags argument must match the one
  323. * used for skb_recv_datagram.
  324. *
  325. * If the MSG_PEEK flag is set, and the packet is still on the
  326. * receive queue of the socket, it will be taken off the queue
  327. * before it is freed.
  328. *
  329. * This function currently only disables BH when acquiring the
  330. * sk_receive_queue lock. Therefore it must not be used in a
  331. * context where that lock is acquired in an IRQ context.
  332. *
  333. * It returns 0 if the packet was removed by us.
  334. */
  335. int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
  336. {
  337. int err = __sk_queue_drop_skb(sk, skb, flags, NULL);
  338. kfree_skb(skb);
  339. sk_mem_reclaim_partial(sk);
  340. return err;
  341. }
  342. EXPORT_SYMBOL(skb_kill_datagram);
  343. /**
  344. * skb_copy_datagram_iter - Copy a datagram to an iovec iterator.
  345. * @skb: buffer to copy
  346. * @offset: offset in the buffer to start copying from
  347. * @to: iovec iterator to copy to
  348. * @len: amount of data to copy from buffer to iovec
  349. */
  350. int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
  351. struct iov_iter *to, int len)
  352. {
  353. int start = skb_headlen(skb);
  354. int i, copy = start - offset;
  355. struct sk_buff *frag_iter;
  356. trace_skb_copy_datagram_iovec(skb, len);
  357. /* Copy header. */
  358. if (copy > 0) {
  359. if (copy > len)
  360. copy = len;
  361. if (copy_to_iter(skb->data + offset, copy, to) != copy)
  362. goto short_copy;
  363. if ((len -= copy) == 0)
  364. return 0;
  365. offset += copy;
  366. }
  367. /* Copy paged appendix. Hmm... why does this look so complicated? */
  368. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  369. int end;
  370. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  371. WARN_ON(start > offset + len);
  372. end = start + skb_frag_size(frag);
  373. if ((copy = end - offset) > 0) {
  374. if (copy > len)
  375. copy = len;
  376. if (copy_page_to_iter(skb_frag_page(frag),
  377. frag->page_offset + offset -
  378. start, copy, to) != copy)
  379. goto short_copy;
  380. if (!(len -= copy))
  381. return 0;
  382. offset += copy;
  383. }
  384. start = end;
  385. }
  386. skb_walk_frags(skb, frag_iter) {
  387. int end;
  388. WARN_ON(start > offset + len);
  389. end = start + frag_iter->len;
  390. if ((copy = end - offset) > 0) {
  391. if (copy > len)
  392. copy = len;
  393. if (skb_copy_datagram_iter(frag_iter, offset - start,
  394. to, copy))
  395. goto fault;
  396. if ((len -= copy) == 0)
  397. return 0;
  398. offset += copy;
  399. }
  400. start = end;
  401. }
  402. if (!len)
  403. return 0;
  404. /* This is not really a user copy fault, but rather someone
  405. * gave us a bogus length on the skb. We should probably
  406. * print a warning here as it may indicate a kernel bug.
  407. */
  408. fault:
  409. return -EFAULT;
  410. short_copy:
  411. if (iov_iter_count(to))
  412. goto fault;
  413. return 0;
  414. }
  415. EXPORT_SYMBOL(skb_copy_datagram_iter);
  416. /**
  417. * skb_copy_datagram_from_iter - Copy a datagram from an iov_iter.
  418. * @skb: buffer to copy
  419. * @offset: offset in the buffer to start copying to
  420. * @from: the copy source
  421. * @len: amount of data to copy to buffer from iovec
  422. *
  423. * Returns 0 or -EFAULT.
  424. */
  425. int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
  426. struct iov_iter *from,
  427. int len)
  428. {
  429. int start = skb_headlen(skb);
  430. int i, copy = start - offset;
  431. struct sk_buff *frag_iter;
  432. /* Copy header. */
  433. if (copy > 0) {
  434. if (copy > len)
  435. copy = len;
  436. if (copy_from_iter(skb->data + offset, copy, from) != copy)
  437. goto fault;
  438. if ((len -= copy) == 0)
  439. return 0;
  440. offset += copy;
  441. }
  442. /* Copy paged appendix. Hmm... why does this look so complicated? */
  443. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  444. int end;
  445. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  446. WARN_ON(start > offset + len);
  447. end = start + skb_frag_size(frag);
  448. if ((copy = end - offset) > 0) {
  449. size_t copied;
  450. if (copy > len)
  451. copy = len;
  452. copied = copy_page_from_iter(skb_frag_page(frag),
  453. frag->page_offset + offset - start,
  454. copy, from);
  455. if (copied != copy)
  456. goto fault;
  457. if (!(len -= copy))
  458. return 0;
  459. offset += copy;
  460. }
  461. start = end;
  462. }
  463. skb_walk_frags(skb, frag_iter) {
  464. int end;
  465. WARN_ON(start > offset + len);
  466. end = start + frag_iter->len;
  467. if ((copy = end - offset) > 0) {
  468. if (copy > len)
  469. copy = len;
  470. if (skb_copy_datagram_from_iter(frag_iter,
  471. offset - start,
  472. from, copy))
  473. goto fault;
  474. if ((len -= copy) == 0)
  475. return 0;
  476. offset += copy;
  477. }
  478. start = end;
  479. }
  480. if (!len)
  481. return 0;
  482. fault:
  483. return -EFAULT;
  484. }
  485. EXPORT_SYMBOL(skb_copy_datagram_from_iter);
  486. /**
  487. * zerocopy_sg_from_iter - Build a zerocopy datagram from an iov_iter
  488. * @skb: buffer to copy
  489. * @from: the source to copy from
  490. *
  491. * The function will first copy up to headlen, and then pin the userspace
  492. * pages and build frags through them.
  493. *
  494. * Returns 0, -EFAULT or -EMSGSIZE.
  495. */
  496. int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *from)
  497. {
  498. int len = iov_iter_count(from);
  499. int copy = min_t(int, skb_headlen(skb), len);
  500. int frag = 0;
  501. /* copy up to skb headlen */
  502. if (skb_copy_datagram_from_iter(skb, 0, from, copy))
  503. return -EFAULT;
  504. while (iov_iter_count(from)) {
  505. struct page *pages[MAX_SKB_FRAGS];
  506. size_t start;
  507. ssize_t copied;
  508. unsigned long truesize;
  509. int n = 0;
  510. if (frag == MAX_SKB_FRAGS)
  511. return -EMSGSIZE;
  512. copied = iov_iter_get_pages(from, pages, ~0U,
  513. MAX_SKB_FRAGS - frag, &start);
  514. if (copied < 0)
  515. return -EFAULT;
  516. iov_iter_advance(from, copied);
  517. truesize = PAGE_ALIGN(copied + start);
  518. skb->data_len += copied;
  519. skb->len += copied;
  520. skb->truesize += truesize;
  521. atomic_add(truesize, &skb->sk->sk_wmem_alloc);
  522. while (copied) {
  523. int size = min_t(int, copied, PAGE_SIZE - start);
  524. skb_fill_page_desc(skb, frag++, pages[n], start, size);
  525. start = 0;
  526. copied -= size;
  527. n++;
  528. }
  529. }
  530. return 0;
  531. }
  532. EXPORT_SYMBOL(zerocopy_sg_from_iter);
  533. static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset,
  534. struct iov_iter *to, int len,
  535. __wsum *csump)
  536. {
  537. int start = skb_headlen(skb);
  538. int i, copy = start - offset;
  539. struct sk_buff *frag_iter;
  540. int pos = 0;
  541. int n;
  542. /* Copy header. */
  543. if (copy > 0) {
  544. if (copy > len)
  545. copy = len;
  546. n = csum_and_copy_to_iter(skb->data + offset, copy, csump, to);
  547. if (n != copy)
  548. goto fault;
  549. if ((len -= copy) == 0)
  550. return 0;
  551. offset += copy;
  552. pos = copy;
  553. }
  554. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  555. int end;
  556. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  557. WARN_ON(start > offset + len);
  558. end = start + skb_frag_size(frag);
  559. if ((copy = end - offset) > 0) {
  560. __wsum csum2 = 0;
  561. struct page *page = skb_frag_page(frag);
  562. u8 *vaddr = kmap(page);
  563. if (copy > len)
  564. copy = len;
  565. n = csum_and_copy_to_iter(vaddr + frag->page_offset +
  566. offset - start, copy,
  567. &csum2, to);
  568. kunmap(page);
  569. if (n != copy)
  570. goto fault;
  571. *csump = csum_block_add(*csump, csum2, pos);
  572. if (!(len -= copy))
  573. return 0;
  574. offset += copy;
  575. pos += copy;
  576. }
  577. start = end;
  578. }
  579. skb_walk_frags(skb, frag_iter) {
  580. int end;
  581. WARN_ON(start > offset + len);
  582. end = start + frag_iter->len;
  583. if ((copy = end - offset) > 0) {
  584. __wsum csum2 = 0;
  585. if (copy > len)
  586. copy = len;
  587. if (skb_copy_and_csum_datagram(frag_iter,
  588. offset - start,
  589. to, copy,
  590. &csum2))
  591. goto fault;
  592. *csump = csum_block_add(*csump, csum2, pos);
  593. if ((len -= copy) == 0)
  594. return 0;
  595. offset += copy;
  596. pos += copy;
  597. }
  598. start = end;
  599. }
  600. if (!len)
  601. return 0;
  602. fault:
  603. return -EFAULT;
  604. }
  605. __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
  606. {
  607. __sum16 sum;
  608. sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
  609. if (likely(!sum)) {
  610. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
  611. !skb->csum_complete_sw)
  612. netdev_rx_csum_fault(skb->dev);
  613. }
  614. if (!skb_shared(skb))
  615. skb->csum_valid = !sum;
  616. return sum;
  617. }
  618. EXPORT_SYMBOL(__skb_checksum_complete_head);
  619. __sum16 __skb_checksum_complete(struct sk_buff *skb)
  620. {
  621. __wsum csum;
  622. __sum16 sum;
  623. csum = skb_checksum(skb, 0, skb->len, 0);
  624. /* skb->csum holds pseudo checksum */
  625. sum = csum_fold(csum_add(skb->csum, csum));
  626. if (likely(!sum)) {
  627. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
  628. !skb->csum_complete_sw)
  629. netdev_rx_csum_fault(skb->dev);
  630. }
  631. if (!skb_shared(skb)) {
  632. /* Save full packet checksum */
  633. skb->csum = csum;
  634. skb->ip_summed = CHECKSUM_COMPLETE;
  635. skb->csum_complete_sw = 1;
  636. skb->csum_valid = !sum;
  637. }
  638. return sum;
  639. }
  640. EXPORT_SYMBOL(__skb_checksum_complete);
  641. /**
  642. * skb_copy_and_csum_datagram_msg - Copy and checksum skb to user iovec.
  643. * @skb: skbuff
  644. * @hlen: hardware length
  645. * @msg: destination
  646. *
  647. * Caller _must_ check that skb will fit to this iovec.
  648. *
  649. * Returns: 0 - success.
  650. * -EINVAL - checksum failure.
  651. * -EFAULT - fault during copy.
  652. */
  653. int skb_copy_and_csum_datagram_msg(struct sk_buff *skb,
  654. int hlen, struct msghdr *msg)
  655. {
  656. __wsum csum;
  657. int chunk = skb->len - hlen;
  658. if (!chunk)
  659. return 0;
  660. if (msg_data_left(msg) < chunk) {
  661. if (__skb_checksum_complete(skb))
  662. goto csum_error;
  663. if (skb_copy_datagram_msg(skb, hlen, msg, chunk))
  664. goto fault;
  665. } else {
  666. csum = csum_partial(skb->data, hlen, skb->csum);
  667. if (skb_copy_and_csum_datagram(skb, hlen, &msg->msg_iter,
  668. chunk, &csum))
  669. goto fault;
  670. if (csum_fold(csum))
  671. goto csum_error;
  672. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE))
  673. netdev_rx_csum_fault(skb->dev);
  674. }
  675. return 0;
  676. csum_error:
  677. return -EINVAL;
  678. fault:
  679. return -EFAULT;
  680. }
  681. EXPORT_SYMBOL(skb_copy_and_csum_datagram_msg);
  682. /**
  683. * datagram_poll - generic datagram poll
  684. * @file: file struct
  685. * @sock: socket
  686. * @wait: poll table
  687. *
  688. * Datagram poll: Again totally generic. This also handles
  689. * sequenced packet sockets providing the socket receive queue
  690. * is only ever holding data ready to receive.
  691. *
  692. * Note: when you _don't_ use this routine for this protocol,
  693. * and you use a different write policy from sock_writeable()
  694. * then please supply your own write_space callback.
  695. */
  696. unsigned int datagram_poll(struct file *file, struct socket *sock,
  697. poll_table *wait)
  698. {
  699. struct sock *sk = sock->sk;
  700. unsigned int mask;
  701. sock_poll_wait(file, sk_sleep(sk), wait);
  702. mask = 0;
  703. /* exceptional events? */
  704. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  705. mask |= POLLERR |
  706. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
  707. if (sk->sk_shutdown & RCV_SHUTDOWN)
  708. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  709. if (sk->sk_shutdown == SHUTDOWN_MASK)
  710. mask |= POLLHUP;
  711. /* readable? */
  712. if (!skb_queue_empty(&sk->sk_receive_queue))
  713. mask |= POLLIN | POLLRDNORM;
  714. /* Connection-based need to check for termination and startup */
  715. if (connection_based(sk)) {
  716. if (sk->sk_state == TCP_CLOSE)
  717. mask |= POLLHUP;
  718. /* connection hasn't started yet? */
  719. if (sk->sk_state == TCP_SYN_SENT)
  720. return mask;
  721. }
  722. /* writable? */
  723. if (sock_writeable(sk))
  724. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  725. else
  726. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  727. return mask;
  728. }
  729. EXPORT_SYMBOL(datagram_poll);