udplite.h 3.8 KB

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