1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 292aa7c65SAlexander Aring #ifndef __6LOWPAN_NHC_H 392aa7c65SAlexander Aring #define __6LOWPAN_NHC_H 492aa7c65SAlexander Aring 592aa7c65SAlexander Aring #include <linux/skbuff.h> 692aa7c65SAlexander Aring #include <linux/rbtree.h> 792aa7c65SAlexander Aring #include <linux/module.h> 892aa7c65SAlexander Aring 992aa7c65SAlexander Aring #include <net/6lowpan.h> 1092aa7c65SAlexander Aring #include <net/ipv6.h> 1192aa7c65SAlexander Aring 1292aa7c65SAlexander Aring /** 1392aa7c65SAlexander Aring * LOWPAN_NHC - helper macro to generate nh id fields and lowpan_nhc struct 1492aa7c65SAlexander Aring * 1592aa7c65SAlexander Aring * @__nhc: variable name of the lowpan_nhc struct. 1692aa7c65SAlexander Aring * @_name: const char * of common header compression name. 1792aa7c65SAlexander Aring * @_nexthdr: ipv6 nexthdr field for the header compression. 1892aa7c65SAlexander Aring * @_nexthdrlen: ipv6 nexthdr len for the reserved space. 19*31264f95SAlexander Aring * @_id: one byte nhc id value. 20*31264f95SAlexander Aring * @_idmask: one byte nhc id mask value. 2192aa7c65SAlexander Aring * @_uncompress: callback for uncompression call. 2292aa7c65SAlexander Aring * @_compress: callback for compression call. 2392aa7c65SAlexander Aring */ 2492aa7c65SAlexander Aring #define LOWPAN_NHC(__nhc, _name, _nexthdr, \ 25*31264f95SAlexander Aring _hdrlen, _id, _idmask, \ 2692aa7c65SAlexander Aring _uncompress, _compress) \ 2792aa7c65SAlexander Aring static struct lowpan_nhc __nhc = { \ 2892aa7c65SAlexander Aring .name = _name, \ 2992aa7c65SAlexander Aring .nexthdr = _nexthdr, \ 3092aa7c65SAlexander Aring .nexthdrlen = _hdrlen, \ 31*31264f95SAlexander Aring .id = _id, \ 32*31264f95SAlexander Aring .idmask = _idmask, \ 3392aa7c65SAlexander Aring .uncompress = _uncompress, \ 3492aa7c65SAlexander Aring .compress = _compress, \ 3592aa7c65SAlexander Aring } 3692aa7c65SAlexander Aring 3792aa7c65SAlexander Aring #define module_lowpan_nhc(__nhc) \ 3892aa7c65SAlexander Aring static int __init __nhc##_init(void) \ 3992aa7c65SAlexander Aring { \ 4092aa7c65SAlexander Aring return lowpan_nhc_add(&(__nhc)); \ 4192aa7c65SAlexander Aring } \ 4292aa7c65SAlexander Aring module_init(__nhc##_init); \ 4392aa7c65SAlexander Aring static void __exit __nhc##_exit(void) \ 4492aa7c65SAlexander Aring { \ 4592aa7c65SAlexander Aring lowpan_nhc_del(&(__nhc)); \ 4692aa7c65SAlexander Aring } \ 4792aa7c65SAlexander Aring module_exit(__nhc##_exit); 4892aa7c65SAlexander Aring 4992aa7c65SAlexander Aring /** 5092aa7c65SAlexander Aring * struct lowpan_nhc - hold 6lowpan next hdr compression ifnformation 5192aa7c65SAlexander Aring * 5292aa7c65SAlexander Aring * @name: name of the specific next header compression 5392aa7c65SAlexander Aring * @nexthdr: next header value of the protocol which should be compressed. 5492aa7c65SAlexander Aring * @nexthdrlen: ipv6 nexthdr len for the reserved space. 55*31264f95SAlexander Aring * @id: one byte nhc id value. 56*31264f95SAlexander Aring * @idmask: one byte nhc id mask value. 5792aa7c65SAlexander Aring * @compress: callback to do the header compression. 5892aa7c65SAlexander Aring * @uncompress: callback to do the header uncompression. 5992aa7c65SAlexander Aring */ 6092aa7c65SAlexander Aring struct lowpan_nhc { 6192aa7c65SAlexander Aring const char *name; 62eb9edf43SAlexander Aring u8 nexthdr; 63eb9edf43SAlexander Aring size_t nexthdrlen; 64*31264f95SAlexander Aring u8 id; 65*31264f95SAlexander Aring u8 idmask; 6692aa7c65SAlexander Aring 6792aa7c65SAlexander Aring int (*uncompress)(struct sk_buff *skb, size_t needed); 6892aa7c65SAlexander Aring int (*compress)(struct sk_buff *skb, u8 **hc_ptr); 6992aa7c65SAlexander Aring }; 7092aa7c65SAlexander Aring 7192aa7c65SAlexander Aring /** 7292aa7c65SAlexander Aring * lowpan_nhc_by_nexthdr - return the 6lowpan nhc by ipv6 nexthdr. 7392aa7c65SAlexander Aring * 7492aa7c65SAlexander Aring * @nexthdr: ipv6 nexthdr value. 7592aa7c65SAlexander Aring */ 7692aa7c65SAlexander Aring struct lowpan_nhc *lowpan_nhc_by_nexthdr(u8 nexthdr); 7792aa7c65SAlexander Aring 7892aa7c65SAlexander Aring /** 7992aa7c65SAlexander Aring * lowpan_nhc_check_compression - checks if we support compression format. If 80607b0bd3SAlexander Aring * we support the nhc by nexthdr field, the function will return 0. If we 81607b0bd3SAlexander Aring * don't support the nhc by nexthdr this function will return -ENOENT. 8292aa7c65SAlexander Aring * 8392aa7c65SAlexander Aring * @skb: skb of 6LoWPAN header to read nhc and replace header. 8492aa7c65SAlexander Aring * @hdr: ipv6hdr to check the nexthdr value 8592aa7c65SAlexander Aring * @hc_ptr: pointer for 6LoWPAN header which should increment at the end of 8692aa7c65SAlexander Aring * replaced header. 8792aa7c65SAlexander Aring */ 8892aa7c65SAlexander Aring int lowpan_nhc_check_compression(struct sk_buff *skb, 89607b0bd3SAlexander Aring const struct ipv6hdr *hdr, u8 **hc_ptr); 9092aa7c65SAlexander Aring 9192aa7c65SAlexander Aring /** 9292aa7c65SAlexander Aring * lowpan_nhc_do_compression - calling compress callback for nhc 9392aa7c65SAlexander Aring * 9492aa7c65SAlexander Aring * @skb: skb of 6LoWPAN header to read nhc and replace header. 9592aa7c65SAlexander Aring * @hdr: ipv6hdr to set the nexthdr value 9692aa7c65SAlexander Aring * @hc_ptr: pointer for 6LoWPAN header which should increment at the end of 9792aa7c65SAlexander Aring * replaced header. 9892aa7c65SAlexander Aring */ 9992aa7c65SAlexander Aring int lowpan_nhc_do_compression(struct sk_buff *skb, const struct ipv6hdr *hdr, 10092aa7c65SAlexander Aring u8 **hc_ptr); 10192aa7c65SAlexander Aring 10292aa7c65SAlexander Aring /** 10392aa7c65SAlexander Aring * lowpan_nhc_do_uncompression - calling uncompress callback for nhc 10492aa7c65SAlexander Aring * 10592aa7c65SAlexander Aring * @nhc: 6LoWPAN nhc context, get by lowpan_nhc_by_ functions. 10692aa7c65SAlexander Aring * @skb: skb of 6LoWPAN header, skb->data should be pointed to nhc id value. 10792aa7c65SAlexander Aring * @dev: netdevice for print logging information. 10892aa7c65SAlexander Aring * @hdr: ipv6hdr for setting nexthdr value. 10992aa7c65SAlexander Aring */ 1108911d774SAlexander Aring int lowpan_nhc_do_uncompression(struct sk_buff *skb, 1118911d774SAlexander Aring const struct net_device *dev, 11292aa7c65SAlexander Aring struct ipv6hdr *hdr); 11392aa7c65SAlexander Aring 11492aa7c65SAlexander Aring /** 11592aa7c65SAlexander Aring * lowpan_nhc_add - register a next header compression to framework 11692aa7c65SAlexander Aring * 11792aa7c65SAlexander Aring * @nhc: nhc which should be add. 11892aa7c65SAlexander Aring */ 11992aa7c65SAlexander Aring int lowpan_nhc_add(struct lowpan_nhc *nhc); 12092aa7c65SAlexander Aring 12192aa7c65SAlexander Aring /** 12292aa7c65SAlexander Aring * lowpan_nhc_del - delete a next header compression from framework 12392aa7c65SAlexander Aring * 12492aa7c65SAlexander Aring * @nhc: nhc which should be delete. 12592aa7c65SAlexander Aring */ 12692aa7c65SAlexander Aring void lowpan_nhc_del(struct lowpan_nhc *nhc); 12792aa7c65SAlexander Aring 12892aa7c65SAlexander Aring /** 12992aa7c65SAlexander Aring * lowpan_nhc_init - adding all default nhcs 13092aa7c65SAlexander Aring */ 13192aa7c65SAlexander Aring void lowpan_nhc_init(void); 13292aa7c65SAlexander Aring 13392aa7c65SAlexander Aring #endif /* __6LOWPAN_NHC_H */ 134