dsfield.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* include/net/dsfield.h - Manipulation of the Differentiated Services field */
  3. /* Written 1998-2000 by Werner Almesberger, EPFL ICA */
  4. #ifndef __NET_DSFIELD_H
  5. #define __NET_DSFIELD_H
  6. #include <linux/types.h>
  7. #include <linux/ip.h>
  8. #include <linux/ipv6.h>
  9. #include <asm/byteorder.h>
  10. static inline __u8 ipv4_get_dsfield(const struct iphdr *iph)
  11. {
  12. return iph->tos;
  13. }
  14. static inline __u8 ipv6_get_dsfield(const struct ipv6hdr *ipv6h)
  15. {
  16. return ntohs(*(const __be16 *)ipv6h) >> 4;
  17. }
  18. static inline void ipv4_change_dsfield(struct iphdr *iph,__u8 mask,
  19. __u8 value)
  20. {
  21. __u32 check = ntohs((__force __be16)iph->check);
  22. __u8 dsfield;
  23. dsfield = (iph->tos & mask) | value;
  24. check += iph->tos;
  25. if ((check+1) >> 16) check = (check+1) & 0xffff;
  26. check -= dsfield;
  27. check += check >> 16; /* adjust carry */
  28. iph->check = (__force __sum16)htons(check);
  29. iph->tos = dsfield;
  30. }
  31. static inline void ipv6_change_dsfield(struct ipv6hdr *ipv6h,__u8 mask,
  32. __u8 value)
  33. {
  34. __be16 *p = (__force __be16 *)ipv6h;
  35. *p = (*p & htons((((u16)mask << 4) | 0xf00f))) | htons((u16)value << 4);
  36. }
  37. #endif