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