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