nhc.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * @_id: one byte nhc id value.
  17. * @_idmask: one byte nhc id mask value.
  18. * @_uncompress: callback for uncompression call.
  19. * @_compress: callback for compression call.
  20. */
  21. #define LOWPAN_NHC(__nhc, _name, _nexthdr, \
  22. _hdrlen, _id, _idmask, \
  23. _uncompress, _compress) \
  24. static const struct lowpan_nhc __nhc = { \
  25. .name = _name, \
  26. .nexthdr = _nexthdr, \
  27. .nexthdrlen = _hdrlen, \
  28. .id = _id, \
  29. .idmask = _idmask, \
  30. .uncompress = _uncompress, \
  31. .compress = _compress, \
  32. }
  33. #define module_lowpan_nhc(__nhc) \
  34. static int __init __nhc##_init(void) \
  35. { \
  36. return lowpan_nhc_add(&(__nhc)); \
  37. } \
  38. module_init(__nhc##_init); \
  39. static void __exit __nhc##_exit(void) \
  40. { \
  41. lowpan_nhc_del(&(__nhc)); \
  42. } \
  43. module_exit(__nhc##_exit);
  44. /**
  45. * struct lowpan_nhc - hold 6lowpan next hdr compression ifnformation
  46. *
  47. * @name: name of the specific next header compression
  48. * @nexthdr: next header value of the protocol which should be compressed.
  49. * @nexthdrlen: ipv6 nexthdr len for the reserved space.
  50. * @id: one byte nhc id value.
  51. * @idmask: one byte nhc id mask value.
  52. * @compress: callback to do the header compression.
  53. * @uncompress: callback to do the header uncompression.
  54. */
  55. struct lowpan_nhc {
  56. const char *name;
  57. u8 nexthdr;
  58. size_t nexthdrlen;
  59. u8 id;
  60. u8 idmask;
  61. int (*uncompress)(struct sk_buff *skb, size_t needed);
  62. int (*compress)(struct sk_buff *skb, u8 **hc_ptr);
  63. };
  64. /**
  65. * lowpan_nhc_by_nexthdr - return the 6lowpan nhc by ipv6 nexthdr.
  66. *
  67. * @nexthdr: ipv6 nexthdr value.
  68. */
  69. struct lowpan_nhc *lowpan_nhc_by_nexthdr(u8 nexthdr);
  70. /**
  71. * lowpan_nhc_check_compression - checks if we support compression format. If
  72. * we support the nhc by nexthdr field, the function will return 0. If we
  73. * don't support the nhc by nexthdr this function will return -ENOENT.
  74. *
  75. * @skb: skb of 6LoWPAN header to read nhc and replace header.
  76. * @hdr: ipv6hdr to check the nexthdr value
  77. * @hc_ptr: pointer for 6LoWPAN header which should increment at the end of
  78. * replaced header.
  79. */
  80. int lowpan_nhc_check_compression(struct sk_buff *skb,
  81. const struct ipv6hdr *hdr, u8 **hc_ptr);
  82. /**
  83. * lowpan_nhc_do_compression - calling compress callback for nhc
  84. *
  85. * @skb: skb of 6LoWPAN header to read nhc and replace header.
  86. * @hdr: ipv6hdr to set 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_do_compression(struct sk_buff *skb, const struct ipv6hdr *hdr,
  91. u8 **hc_ptr);
  92. /**
  93. * lowpan_nhc_do_uncompression - calling uncompress callback for nhc
  94. *
  95. * @nhc: 6LoWPAN nhc context, get by lowpan_nhc_by_ functions.
  96. * @skb: skb of 6LoWPAN header, skb->data should be pointed to nhc id value.
  97. * @dev: netdevice for print logging information.
  98. * @hdr: ipv6hdr for setting nexthdr value.
  99. */
  100. int lowpan_nhc_do_uncompression(struct sk_buff *skb,
  101. const struct net_device *dev,
  102. struct ipv6hdr *hdr);
  103. /**
  104. * lowpan_nhc_add - register a next header compression to framework
  105. *
  106. * @nhc: nhc which should be add.
  107. */
  108. int lowpan_nhc_add(const struct lowpan_nhc *nhc);
  109. /**
  110. * lowpan_nhc_del - delete a next header compression from framework
  111. *
  112. * @nhc: nhc which should be delete.
  113. */
  114. void lowpan_nhc_del(const struct lowpan_nhc *nhc);
  115. /**
  116. * lowpan_nhc_init - adding all default nhcs
  117. */
  118. void lowpan_nhc_init(void);
  119. #endif /* __6LOWPAN_NHC_H */