nhc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __6LOWPAN_NHC_H
  3. #define __6LOWPAN_NHC_H
  4. #include <linux/skbuff.h>
  5. #include <linux/rbtree.h>
  6. #include <linux/module.h>
  7. #include <net/6lowpan.h>
  8. #include <net/ipv6.h>
  9. /**
  10. * LOWPAN_NHC - helper macro to generate nh id fields and lowpan_nhc struct
  11. *
  12. * @__nhc: variable name of the lowpan_nhc struct.
  13. * @_name: const char * of common header compression name.
  14. * @_nexthdr: ipv6 nexthdr field for the header compression.
  15. * @_nexthdrlen: ipv6 nexthdr len for the reserved space.
  16. * @_idsetup: callback to setup id and mask values.
  17. * @_idlen: len for the next header id and mask, should be always the same.
  18. * @_uncompress: callback for uncompression call.
  19. * @_compress: callback for compression call.
  20. */
  21. #define LOWPAN_NHC(__nhc, _name, _nexthdr, \
  22. _hdrlen, _idsetup, _idlen, \
  23. _uncompress, _compress) \
  24. static u8 __nhc##_val[_idlen]; \
  25. static u8 __nhc##_mask[_idlen]; \
  26. static struct lowpan_nhc __nhc = { \
  27. .name = _name, \
  28. .nexthdr = _nexthdr, \
  29. .nexthdrlen = _hdrlen, \
  30. .id = __nhc##_val, \
  31. .idmask = __nhc##_mask, \
  32. .idlen = _idlen, \
  33. .idsetup = _idsetup, \
  34. .uncompress = _uncompress, \
  35. .compress = _compress, \
  36. }
  37. #define module_lowpan_nhc(__nhc) \
  38. static int __init __nhc##_init(void) \
  39. { \
  40. return lowpan_nhc_add(&(__nhc)); \
  41. } \
  42. module_init(__nhc##_init); \
  43. static void __exit __nhc##_exit(void) \
  44. { \
  45. lowpan_nhc_del(&(__nhc)); \
  46. } \
  47. module_exit(__nhc##_exit);
  48. /**
  49. * struct lowpan_nhc - hold 6lowpan next hdr compression ifnformation
  50. *
  51. * @node: holder for the rbtree.
  52. * @name: name of the specific next header compression
  53. * @nexthdr: next header value of the protocol which should be compressed.
  54. * @nexthdrlen: ipv6 nexthdr len for the reserved space.
  55. * @id: array for nhc id. Note this need to be in network byteorder.
  56. * @mask: array for nhc id mask. Note this need to be in network byteorder.
  57. * @len: the length of the next header id and mask.
  58. * @setup: callback to setup fill the next header id value and mask.
  59. * @compress: callback to do the header compression.
  60. * @uncompress: callback to do the header uncompression.
  61. */
  62. struct lowpan_nhc {
  63. struct rb_node node;
  64. const char *name;
  65. const u8 nexthdr;
  66. const size_t nexthdrlen;
  67. u8 *id;
  68. u8 *idmask;
  69. const size_t idlen;
  70. void (*idsetup)(struct lowpan_nhc *nhc);
  71. int (*uncompress)(struct sk_buff *skb, size_t needed);
  72. int (*compress)(struct sk_buff *skb, u8 **hc_ptr);
  73. };
  74. /**
  75. * lowpan_nhc_by_nexthdr - return the 6lowpan nhc by ipv6 nexthdr.
  76. *
  77. * @nexthdr: ipv6 nexthdr value.
  78. */
  79. struct lowpan_nhc *lowpan_nhc_by_nexthdr(u8 nexthdr);
  80. /**
  81. * lowpan_nhc_check_compression - checks if we support compression format. If
  82. * we support the nhc by nexthdr field, the function will return 0. If we
  83. * don't support the nhc by nexthdr this function will return -ENOENT.
  84. *
  85. * @skb: skb of 6LoWPAN header to read nhc and replace header.
  86. * @hdr: ipv6hdr to check the nexthdr value
  87. * @hc_ptr: pointer for 6LoWPAN header which should increment at the end of
  88. * replaced header.
  89. */
  90. int lowpan_nhc_check_compression(struct sk_buff *skb,
  91. const struct ipv6hdr *hdr, u8 **hc_ptr);
  92. /**
  93. * lowpan_nhc_do_compression - calling compress callback for nhc
  94. *
  95. * @skb: skb of 6LoWPAN header to read nhc and replace header.
  96. * @hdr: ipv6hdr to set the nexthdr value
  97. * @hc_ptr: pointer for 6LoWPAN header which should increment at the end of
  98. * replaced header.
  99. */
  100. int lowpan_nhc_do_compression(struct sk_buff *skb, const struct ipv6hdr *hdr,
  101. u8 **hc_ptr);
  102. /**
  103. * lowpan_nhc_do_uncompression - calling uncompress callback for nhc
  104. *
  105. * @nhc: 6LoWPAN nhc context, get by lowpan_nhc_by_ functions.
  106. * @skb: skb of 6LoWPAN header, skb->data should be pointed to nhc id value.
  107. * @dev: netdevice for print logging information.
  108. * @hdr: ipv6hdr for setting nexthdr value.
  109. */
  110. int lowpan_nhc_do_uncompression(struct sk_buff *skb,
  111. const struct net_device *dev,
  112. struct ipv6hdr *hdr);
  113. /**
  114. * lowpan_nhc_add - register a next header compression to framework
  115. *
  116. * @nhc: nhc which should be add.
  117. */
  118. int lowpan_nhc_add(struct lowpan_nhc *nhc);
  119. /**
  120. * lowpan_nhc_del - delete a next header compression from framework
  121. *
  122. * @nhc: nhc which should be delete.
  123. */
  124. void lowpan_nhc_del(struct lowpan_nhc *nhc);
  125. /**
  126. * lowpan_nhc_init - adding all default nhcs
  127. */
  128. void lowpan_nhc_init(void);
  129. #endif /* __6LOWPAN_NHC_H */