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