nhc.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef __6LOWPAN_NHC_H
  2. #define __6LOWPAN_NHC_H
  3. #include <linux/skbuff.h>
  4. #include <linux/rbtree.h>
  5. #include <linux/module.h>
  6. #include <net/6lowpan.h>
  7. #include <net/ipv6.h>
  8. #define LOWPAN_NHC_MAX_ID_LEN 1
  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 6LoWPAN iphc NHC bit will be
  83. * set. If we don't support nexthdr will be added as inline data to the
  84. * 6LoWPAN header.
  85. *
  86. * @skb: skb of 6LoWPAN header to read nhc and replace header.
  87. * @hdr: ipv6hdr to check the nexthdr value
  88. * @hc_ptr: pointer for 6LoWPAN header which should increment at the end of
  89. * replaced header.
  90. * @iphc0: iphc0 pointer to set the 6LoWPAN NHC bit
  91. */
  92. int lowpan_nhc_check_compression(struct sk_buff *skb,
  93. const struct ipv6hdr *hdr, u8 **hc_ptr,
  94. u8 *iphc0);
  95. /**
  96. * lowpan_nhc_do_compression - calling compress callback for nhc
  97. *
  98. * @skb: skb of 6LoWPAN header to read nhc and replace header.
  99. * @hdr: ipv6hdr to set the nexthdr value
  100. * @hc_ptr: pointer for 6LoWPAN header which should increment at the end of
  101. * replaced header.
  102. */
  103. int lowpan_nhc_do_compression(struct sk_buff *skb, const struct ipv6hdr *hdr,
  104. u8 **hc_ptr);
  105. /**
  106. * lowpan_nhc_do_uncompression - calling uncompress callback for nhc
  107. *
  108. * @nhc: 6LoWPAN nhc context, get by lowpan_nhc_by_ functions.
  109. * @skb: skb of 6LoWPAN header, skb->data should be pointed to nhc id value.
  110. * @dev: netdevice for print logging information.
  111. * @hdr: ipv6hdr for setting nexthdr value.
  112. */
  113. int lowpan_nhc_do_uncompression(struct sk_buff *skb, struct net_device *dev,
  114. struct ipv6hdr *hdr);
  115. /**
  116. * lowpan_nhc_add - register a next header compression to framework
  117. *
  118. * @nhc: nhc which should be add.
  119. */
  120. int lowpan_nhc_add(struct lowpan_nhc *nhc);
  121. /**
  122. * lowpan_nhc_del - delete a next header compression from framework
  123. *
  124. * @nhc: nhc which should be delete.
  125. */
  126. void lowpan_nhc_del(struct lowpan_nhc *nhc);
  127. /**
  128. * lowpan_nhc_init - adding all default nhcs
  129. */
  130. void lowpan_nhc_init(void);
  131. #endif /* __6LOWPAN_NHC_H */