udplite.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /* UDP-Lite socket options */
  9. #define UDPLITE_SEND_CSCOV 10 /* sender partial coverage (as sent) */
  10. #define UDPLITE_RECV_CSCOV 11 /* receiver partial coverage (threshold ) */
  11. extern struct proto udplite_prot;
  12. extern struct udp_table udplite_table;
  13. /*
  14. * Checksum computation is all in software, hence simpler getfrag.
  15. */
  16. static __inline__ int udplite_getfrag(void *from, char *to, int offset,
  17. int len, int odd, struct sk_buff *skb)
  18. {
  19. struct msghdr *msg = from;
  20. return copy_from_iter_full(to, len, &msg->msg_iter) ? 0 : -EFAULT;
  21. }
  22. /* Designate sk as UDP-Lite socket */
  23. static inline int udplite_sk_init(struct sock *sk)
  24. {
  25. udp_init_sock(sk);
  26. udp_sk(sk)->pcflag = UDPLITE_BIT;
  27. return 0;
  28. }
  29. /*
  30. * Checksumming routines
  31. */
  32. static inline int udplite_checksum_init(struct sk_buff *skb, struct udphdr *uh)
  33. {
  34. u16 cscov;
  35. /* In UDPv4 a zero checksum means that the transmitter generated no
  36. * checksum. UDP-Lite (like IPv6) mandates checksums, hence packets
  37. * with a zero checksum field are illegal. */
  38. if (uh->check == 0) {
  39. net_dbg_ratelimited("UDPLite: zeroed checksum field\n");
  40. return 1;
  41. }
  42. cscov = ntohs(uh->len);
  43. if (cscov == 0) /* Indicates that full coverage is required. */
  44. ;
  45. else if (cscov < 8 || cscov > skb->len) {
  46. /*
  47. * Coverage length violates RFC 3828: log and discard silently.
  48. */
  49. net_dbg_ratelimited("UDPLite: bad csum coverage %d/%d\n",
  50. cscov, skb->len);
  51. return 1;
  52. } else if (cscov < skb->len) {
  53. UDP_SKB_CB(skb)->partial_cov = 1;
  54. UDP_SKB_CB(skb)->cscov = cscov;
  55. if (skb->ip_summed == CHECKSUM_COMPLETE)
  56. skb->ip_summed = CHECKSUM_NONE;
  57. }
  58. return 0;
  59. }
  60. /* Slow-path computation of checksum. Socket is locked. */
  61. static inline __wsum udplite_csum_outgoing(struct sock *sk, struct sk_buff *skb)
  62. {
  63. const struct udp_sock *up = udp_sk(skb->sk);
  64. int cscov = up->len;
  65. __wsum csum = 0;
  66. if (up->pcflag & UDPLITE_SEND_CC) {
  67. /*
  68. * Sender has set `partial coverage' option on UDP-Lite socket.
  69. * The special case "up->pcslen == 0" signifies full coverage.
  70. */
  71. if (up->pcslen < up->len) {
  72. if (0 < up->pcslen)
  73. cscov = up->pcslen;
  74. udp_hdr(skb)->len = htons(up->pcslen);
  75. }
  76. /*
  77. * NOTE: Causes for the error case `up->pcslen > up->len':
  78. * (i) Application error (will not be penalized).
  79. * (ii) Payload too big for send buffer: data is split
  80. * into several packets, each with its own header.
  81. * In this case (e.g. last segment), coverage may
  82. * exceed packet length.
  83. * Since packets with coverage length > packet length are
  84. * illegal, we fall back to the defaults here.
  85. */
  86. }
  87. skb->ip_summed = CHECKSUM_NONE; /* no HW support for checksumming */
  88. skb_queue_walk(&sk->sk_write_queue, skb) {
  89. const int off = skb_transport_offset(skb);
  90. const int len = skb->len - off;
  91. csum = skb_checksum(skb, off, (cscov > len)? len : cscov, csum);
  92. if ((cscov -= len) <= 0)
  93. break;
  94. }
  95. return csum;
  96. }
  97. /* Fast-path computation of checksum. Socket may not be locked. */
  98. static inline __wsum udplite_csum(struct sk_buff *skb)
  99. {
  100. const struct udp_sock *up = udp_sk(skb->sk);
  101. const int off = skb_transport_offset(skb);
  102. int len = skb->len - off;
  103. if ((up->pcflag & UDPLITE_SEND_CC) && up->pcslen < len) {
  104. if (0 < up->pcslen)
  105. len = up->pcslen;
  106. udp_hdr(skb)->len = htons(up->pcslen);
  107. }
  108. skb->ip_summed = CHECKSUM_NONE; /* no HW support for checksumming */
  109. return skb_checksum(skb, off, len, 0);
  110. }
  111. void udplite4_register(void);
  112. int udplite_get_port(struct sock *sk, unsigned short snum,
  113. int (*scmp)(const struct sock *, const struct sock *));
  114. #endif /* _UDPLITE_H */