1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __6LOWPAN_NHC_H
3 #define __6LOWPAN_NHC_H
4 
5 #include <linux/skbuff.h>
6 #include <linux/rbtree.h>
7 #include <linux/module.h>
8 
9 #include <net/6lowpan.h>
10 #include <net/ipv6.h>
11 
12 /**
13  * LOWPAN_NHC - helper macro to generate nh id fields and lowpan_nhc struct
14  *
15  * @__nhc: variable name of the lowpan_nhc struct.
16  * @_name: const char * of common header compression name.
17  * @_nexthdr: ipv6 nexthdr field for the header compression.
18  * @_nexthdrlen: ipv6 nexthdr len for the reserved space.
19  * @_id: one byte nhc id value.
20  * @_idmask: one byte nhc id mask value.
21  * @_uncompress: callback for uncompression call.
22  * @_compress: callback for compression call.
23  */
24 #define LOWPAN_NHC(__nhc, _name, _nexthdr,	\
25 		   _hdrlen, _id, _idmask,	\
26 		   _uncompress, _compress)	\
27 static const struct lowpan_nhc __nhc = {	\
28 	.name		= _name,		\
29 	.nexthdr	= _nexthdr,		\
30 	.nexthdrlen	= _hdrlen,		\
31 	.id		= _id,			\
32 	.idmask		= _idmask,		\
33 	.uncompress	= _uncompress,		\
34 	.compress	= _compress,		\
35 }
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 /**
50  * struct lowpan_nhc - hold 6lowpan next hdr compression ifnformation
51  *
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: one byte nhc id value.
56  * @idmask: one byte nhc id mask value.
57  * @compress: callback to do the header compression.
58  * @uncompress: callback to do the header uncompression.
59  */
60 struct lowpan_nhc {
61 	const char	*name;
62 	u8		nexthdr;
63 	size_t		nexthdrlen;
64 	u8		id;
65 	u8		idmask;
66 
67 	int		(*uncompress)(struct sk_buff *skb, size_t needed);
68 	int		(*compress)(struct sk_buff *skb, u8 **hc_ptr);
69 };
70 
71 /**
72  * lowpan_nhc_by_nexthdr - return the 6lowpan nhc by ipv6 nexthdr.
73  *
74  * @nexthdr: ipv6 nexthdr value.
75  */
76 struct lowpan_nhc *lowpan_nhc_by_nexthdr(u8 nexthdr);
77 
78 /**
79  * lowpan_nhc_check_compression - checks if we support compression format. If
80  *	we support the nhc by nexthdr field, the function will return 0. If we
81  *	don't support the nhc by nexthdr this function will return -ENOENT.
82  *
83  * @skb: skb of 6LoWPAN header to read nhc and replace header.
84  * @hdr: ipv6hdr to check the nexthdr value
85  * @hc_ptr: pointer for 6LoWPAN header which should increment at the end of
86  *	    replaced header.
87  */
88 int lowpan_nhc_check_compression(struct sk_buff *skb,
89 				 const struct ipv6hdr *hdr, u8 **hc_ptr);
90 
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 /**
103  * lowpan_nhc_do_uncompression - calling uncompress callback for nhc
104  *
105  * @nhc: 6LoWPAN nhc context, get by lowpan_nhc_by_ functions.
106  * @skb: skb of 6LoWPAN header, skb->data should be pointed to nhc id value.
107  * @dev: netdevice for print logging information.
108  * @hdr: ipv6hdr for setting nexthdr value.
109  */
110 int lowpan_nhc_do_uncompression(struct sk_buff *skb,
111 				const struct net_device *dev,
112 				struct ipv6hdr *hdr);
113 
114 /**
115  * lowpan_nhc_add - register a next header compression to framework
116  *
117  * @nhc: nhc which should be add.
118  */
119 int lowpan_nhc_add(const struct lowpan_nhc *nhc);
120 
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(const struct lowpan_nhc *nhc);
127 
128 /**
129  * lowpan_nhc_init - adding all default nhcs
130  */
131 void lowpan_nhc_init(void);
132 
133 #endif /* __6LOWPAN_NHC_H */
134