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