udplite.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 hlist_head udplite_hash[UDP_HTABLE_SIZE];
  12. /* UDP-Lite does not have a standardized MIB yet, so we inherit from UDP */
  13. DECLARE_SNMP_STAT(struct udp_mib, udplite_statistics);
  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. return memcpy_fromiovecend(to, (struct iovec *) from, offset, len);
  21. }
  22. /* Designate sk as UDP-Lite socket */
  23. static inline int udplite_sk_init(struct sock *sk)
  24. {
  25. udp_sk(sk)->pcflag = UDPLITE_BIT;
  26. return 0;
  27. }
  28. /*
  29. * Checksumming routines
  30. */
  31. static inline int udplite_checksum_init(struct sk_buff *skb, struct udphdr *uh)
  32. {
  33. u16 cscov;
  34. /* In UDPv4 a zero checksum means that the transmitter generated no
  35. * checksum. UDP-Lite (like IPv6) mandates checksums, hence packets
  36. * with a zero checksum field are illegal. */
  37. if (uh->check == 0) {
  38. LIMIT_NETDEBUG(KERN_DEBUG "UDPLITE: zeroed checksum field\n");
  39. return 1;
  40. }
  41. UDP_SKB_CB(skb)->partial_cov = 0;
  42. cscov = ntohs(uh->len);
  43. if (cscov == 0) /* Indicates that full coverage is required. */
  44. cscov = skb->len;
  45. else if (cscov < 8 || cscov > skb->len) {
  46. /*
  47. * Coverage length violates RFC 3828: log and discard silently.
  48. */
  49. LIMIT_NETDEBUG(KERN_DEBUG "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. /*
  56. * There is no known NIC manufacturer supporting UDP-Lite yet,
  57. * hence ip_summed is always (re-)set to CHECKSUM_NONE.
  58. */
  59. skb->ip_summed = CHECKSUM_NONE;
  60. return 0;
  61. }
  62. static __inline__ int udplite4_csum_init(struct sk_buff *skb, struct udphdr *uh)
  63. {
  64. int rc = udplite_checksum_init(skb, uh);
  65. if (!rc)
  66. skb->csum = csum_tcpudp_nofold(skb->nh.iph->saddr,
  67. skb->nh.iph->daddr,
  68. skb->len, IPPROTO_UDPLITE, 0);
  69. return rc;
  70. }
  71. static __inline__ int udplite6_csum_init(struct sk_buff *skb, struct udphdr *uh)
  72. {
  73. int rc = udplite_checksum_init(skb, uh);
  74. if (!rc)
  75. skb->csum = ~csum_unfold(csum_ipv6_magic(&skb->nh.ipv6h->saddr,
  76. &skb->nh.ipv6h->daddr,
  77. skb->len, IPPROTO_UDPLITE, 0));
  78. return rc;
  79. }
  80. static inline int udplite_sender_cscov(struct udp_sock *up, struct udphdr *uh)
  81. {
  82. int cscov = up->len;
  83. /*
  84. * Sender has set `partial coverage' option on UDP-Lite socket
  85. */
  86. if (up->pcflag & UDPLITE_SEND_CC) {
  87. if (up->pcslen < up->len) {
  88. /* up->pcslen == 0 means that full coverage is required,
  89. * partial coverage only if 0 < up->pcslen < up->len */
  90. if (0 < up->pcslen) {
  91. cscov = up->pcslen;
  92. }
  93. uh->len = htons(up->pcslen);
  94. }
  95. /*
  96. * NOTE: Causes for the error case `up->pcslen > up->len':
  97. * (i) Application error (will not be penalized).
  98. * (ii) Payload too big for send buffer: data is split
  99. * into several packets, each with its own header.
  100. * In this case (e.g. last segment), coverage may
  101. * exceed packet length.
  102. * Since packets with coverage length > packet length are
  103. * illegal, we fall back to the defaults here.
  104. */
  105. }
  106. return cscov;
  107. }
  108. static inline __wsum udplite_csum_outgoing(struct sock *sk, struct sk_buff *skb)
  109. {
  110. int off, len, cscov = udplite_sender_cscov(udp_sk(sk), skb->h.uh);
  111. __wsum csum = 0;
  112. skb->ip_summed = CHECKSUM_NONE; /* no HW support for checksumming */
  113. skb_queue_walk(&sk->sk_write_queue, skb) {
  114. off = skb->h.raw - skb->data;
  115. len = skb->len - off;
  116. csum = skb_checksum(skb, off, (cscov > len)? len : cscov, csum);
  117. if ((cscov -= len) <= 0)
  118. break;
  119. }
  120. return csum;
  121. }
  122. extern void udplite4_register(void);
  123. extern int udplite_get_port(struct sock *sk, unsigned short snum,
  124. int (*scmp)(const struct sock *, const struct sock *));
  125. #endif /* _UDPLITE_H */