udplite.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Definitions for the UDP-Lite (RFC 3828) code.
  4. */
  5. #ifndef _UDPLITE_H
  6. #define _UDPLITE_H
  7. #include <net/ip6_checksum.h>
  8. #include <net/udp.h>
  9. /* UDP-Lite socket options */
  10. #define UDPLITE_SEND_CSCOV 10 /* sender partial coverage (as sent) */
  11. #define UDPLITE_RECV_CSCOV 11 /* receiver partial coverage (threshold ) */
  12. extern struct proto udplite_prot;
  13. extern struct udp_table udplite_table;
  14. /*
  15. * Checksum computation is all in software, hence simpler getfrag.
  16. */
  17. static __inline__ int udplite_getfrag(void *from, char *to, int offset,
  18. int len, int odd, struct sk_buff *skb)
  19. {
  20. struct msghdr *msg = from;
  21. return copy_from_iter_full(to, len, &msg->msg_iter) ? 0 : -EFAULT;
  22. }
  23. /*
  24. * Checksumming routines
  25. */
  26. static inline int udplite_checksum_init(struct sk_buff *skb, struct udphdr *uh)
  27. {
  28. u16 cscov;
  29. /* In UDPv4 a zero checksum means that the transmitter generated no
  30. * checksum. UDP-Lite (like IPv6) mandates checksums, hence packets
  31. * with a zero checksum field are illegal. */
  32. if (uh->check == 0) {
  33. net_dbg_ratelimited("UDPLite: zeroed checksum field\n");
  34. return 1;
  35. }
  36. cscov = ntohs(uh->len);
  37. if (cscov == 0) /* Indicates that full coverage is required. */
  38. ;
  39. else if (cscov < 8 || cscov > skb->len) {
  40. /*
  41. * Coverage length violates RFC 3828: log and discard silently.
  42. */
  43. net_dbg_ratelimited("UDPLite: bad csum coverage %d/%d\n",
  44. cscov, skb->len);
  45. return 1;
  46. } else if (cscov < skb->len) {
  47. UDP_SKB_CB(skb)->partial_cov = 1;
  48. UDP_SKB_CB(skb)->cscov = cscov;
  49. if (skb->ip_summed == CHECKSUM_COMPLETE)
  50. skb->ip_summed = CHECKSUM_NONE;
  51. skb->csum_valid = 0;
  52. }
  53. return 0;
  54. }
  55. /* Fast-path computation of checksum. Socket may not be locked. */
  56. static inline __wsum udplite_csum(struct sk_buff *skb)
  57. {
  58. const int off = skb_transport_offset(skb);
  59. const struct sock *sk = skb->sk;
  60. int len = skb->len - off;
  61. if (udp_test_bit(UDPLITE_SEND_CC, sk)) {
  62. u16 pcslen = READ_ONCE(udp_sk(sk)->pcslen);
  63. if (pcslen < len) {
  64. if (pcslen > 0)
  65. len = pcslen;
  66. udp_hdr(skb)->len = htons(pcslen);
  67. }
  68. }
  69. skb->ip_summed = CHECKSUM_NONE; /* no HW support for checksumming */
  70. return skb_checksum(skb, off, len, 0);
  71. }
  72. void udplite4_register(void);
  73. #endif /* _UDPLITE_H */