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