nhc.h 4.3 KB

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