udplite.h 4.1 KB

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