socklib.c 4.5 KB

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