Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[firefly-linux-kernel-4.4.55.git] / net / 6lowpan / nhc.h
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 6LoWPAN iphc NHC bit will be
90  *      set. If we don't support nexthdr will be added as inline data to the
91  *      6LoWPAN header.
92  *
93  * @skb: skb of 6LoWPAN header to read nhc and replace header.
94  * @hdr: ipv6hdr to check the nexthdr value
95  * @hc_ptr: pointer for 6LoWPAN header which should increment at the end of
96  *          replaced header.
97  * @iphc0: iphc0 pointer to set the 6LoWPAN NHC bit
98  */
99 int lowpan_nhc_check_compression(struct sk_buff *skb,
100                                  const struct ipv6hdr *hdr, u8 **hc_ptr,
101                                  u8 *iphc0);
102
103 /**
104  * lowpan_nhc_do_compression - calling compress callback for nhc
105  *
106  * @skb: skb of 6LoWPAN header to read nhc and replace header.
107  * @hdr: ipv6hdr to set the nexthdr value
108  * @hc_ptr: pointer for 6LoWPAN header which should increment at the end of
109  *          replaced header.
110  */
111 int lowpan_nhc_do_compression(struct sk_buff *skb, const struct ipv6hdr *hdr,
112                               u8 **hc_ptr);
113
114 /**
115  * lowpan_nhc_do_uncompression - calling uncompress callback for nhc
116  *
117  * @nhc: 6LoWPAN nhc context, get by lowpan_nhc_by_ functions.
118  * @skb: skb of 6LoWPAN header, skb->data should be pointed to nhc id value.
119  * @dev: netdevice for print logging information.
120  * @hdr: ipv6hdr for setting nexthdr value.
121  */
122 int lowpan_nhc_do_uncompression(struct sk_buff *skb, struct net_device *dev,
123                                 struct ipv6hdr *hdr);
124
125 /**
126  * lowpan_nhc_add - register a next header compression to framework
127  *
128  * @nhc: nhc which should be add.
129  */
130 int lowpan_nhc_add(struct lowpan_nhc *nhc);
131
132 /**
133  * lowpan_nhc_del - delete a next header compression from framework
134  *
135  * @nhc: nhc which should be delete.
136  */
137 void lowpan_nhc_del(struct lowpan_nhc *nhc);
138
139 /**
140  * lowpan_nhc_init - adding all default nhcs
141  */
142 void lowpan_nhc_init(void);
143
144 #endif /* __6LOWPAN_NHC_H */