socklib.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * linux/net/sunrpc/socklib.c
  3. *
  4. * Common socket helper routines for RPC client and server
  5. *
  6. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/compiler.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/gfp.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/types.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/udp.h>
  15. #include <linux/sunrpc/xdr.h>
  16. #include <linux/export.h>
  17. /**
  18. * xdr_skb_read_bits - copy some data bits from skb to internal buffer
  19. * @desc: sk_buff copy helper
  20. * @to: copy destination
  21. * @len: number of bytes to copy
  22. *
  23. * Possibly called several times to iterate over an sk_buff and copy
  24. * data out of it.
  25. */
  26. size_t xdr_skb_read_bits(struct xdr_skb_reader *desc, void *to, size_t len)
  27. {
  28. if (len > desc->count)
  29. len = desc->count;
  30. if (unlikely(skb_copy_bits(desc->skb, desc->offset, to, len)))
  31. return 0;
  32. desc->count -= len;
  33. desc->offset += len;
  34. return len;
  35. }
  36. EXPORT_SYMBOL_GPL(xdr_skb_read_bits);
  37. /**
  38. * xdr_skb_read_and_csum_bits - copy and checksum from skb to buffer
  39. * @desc: sk_buff copy helper
  40. * @to: copy destination
  41. * @len: number of bytes to copy
  42. *
  43. * Same as skb_read_bits, but calculate a checksum at the same time.
  44. */
  45. static size_t xdr_skb_read_and_csum_bits(struct xdr_skb_reader *desc, void *to, size_t len)
  46. {
  47. unsigned int pos;
  48. __wsum csum2;
  49. if (len > desc->count)
  50. len = desc->count;
  51. pos = desc->offset;
  52. csum2 = skb_copy_and_csum_bits(desc->skb, pos, to, len, 0);
  53. desc->csum = csum_block_add(desc->csum, csum2, pos);
  54. desc->count -= len;
  55. desc->offset += len;
  56. return len;
  57. }
  58. /**
  59. * xdr_partial_copy_from_skb - copy data out of an skb
  60. * @xdr: target XDR buffer
  61. * @base: starting offset
  62. * @desc: sk_buff copy helper
  63. * @copy_actor: virtual method for copying data
  64. *
  65. */
  66. ssize_t xdr_partial_copy_from_skb(struct xdr_buf *xdr, unsigned int base, struct xdr_skb_reader *desc, xdr_skb_read_actor copy_actor)
  67. {
  68. struct page **ppage = xdr->pages;
  69. unsigned int len, pglen = xdr->page_len;
  70. ssize_t copied = 0;
  71. size_t ret;
  72. len = xdr->head[0].iov_len;
  73. if (base < len) {
  74. len -= base;
  75. ret = copy_actor(desc, (char *)xdr->head[0].iov_base + base, len);
  76. copied += ret;
  77. if (ret != len || !desc->count)
  78. goto out;
  79. base = 0;
  80. } else
  81. base -= len;
  82. if (unlikely(pglen == 0))
  83. goto copy_tail;
  84. if (unlikely(base >= pglen)) {
  85. base -= pglen;
  86. goto copy_tail;
  87. }
  88. if (base || xdr->page_base) {
  89. pglen -= base;
  90. base += xdr->page_base;
  91. ppage += base >> PAGE_SHIFT;
  92. base &= ~PAGE_MASK;
  93. }
  94. do {
  95. char *kaddr;
  96. /* ACL likes to be lazy in allocating pages - ACLs
  97. * are small by default but can get huge. */
  98. if (unlikely(*ppage == NULL)) {
  99. *ppage = alloc_page(GFP_ATOMIC);
  100. if (unlikely(*ppage == NULL)) {
  101. if (copied == 0)
  102. copied = -ENOMEM;
  103. goto out;
  104. }
  105. }
  106. len = PAGE_SIZE;
  107. kaddr = kmap_atomic(*ppage);
  108. if (base) {
  109. len -= base;
  110. if (pglen < len)
  111. len = pglen;
  112. ret = copy_actor(desc, kaddr + base, len);
  113. base = 0;
  114. } else {
  115. if (pglen < len)
  116. len = pglen;
  117. ret = copy_actor(desc, kaddr, len);
  118. }
  119. flush_dcache_page(*ppage);
  120. kunmap_atomic(kaddr);
  121. copied += ret;
  122. if (ret != len || !desc->count)
  123. goto out;
  124. ppage++;
  125. } while ((pglen -= len) != 0);
  126. copy_tail:
  127. len = xdr->tail[0].iov_len;
  128. if (base < len)
  129. copied += copy_actor(desc, (char *)xdr->tail[0].iov_base + base, len - base);
  130. out:
  131. return copied;
  132. }
  133. EXPORT_SYMBOL_GPL(xdr_partial_copy_from_skb);
  134. /**
  135. * csum_partial_copy_to_xdr - checksum and copy data
  136. * @xdr: target XDR buffer
  137. * @skb: source skb
  138. *
  139. * We have set things up such that we perform the checksum of the UDP
  140. * packet in parallel with the copies into the RPC client iovec. -DaveM
  141. */
  142. int csum_partial_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
  143. {
  144. struct xdr_skb_reader desc;
  145. desc.skb = skb;
  146. desc.offset = 0;
  147. desc.count = skb->len - desc.offset;
  148. if (skb_csum_unnecessary(skb))
  149. goto no_checksum;
  150. desc.csum = csum_partial(skb->data, desc.offset, skb->csum);
  151. if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_and_csum_bits) < 0)
  152. return -1;
  153. if (desc.offset != skb->len) {
  154. __wsum csum2;
  155. csum2 = skb_checksum(skb, desc.offset, skb->len - desc.offset, 0);
  156. desc.csum = csum_block_add(desc.csum, csum2, desc.offset);
  157. }
  158. if (desc.count)
  159. return -1;
  160. if (csum_fold(desc.csum))
  161. return -1;
  162. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
  163. !skb->csum_complete_sw)
  164. netdev_rx_csum_fault(skb->dev);
  165. return 0;
  166. no_checksum:
  167. if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
  168. return -1;
  169. if (desc.count)
  170. return -1;
  171. return 0;
  172. }
  173. EXPORT_SYMBOL_GPL(csum_partial_copy_to_xdr);