xref: /openbmc/linux/include/net/6lowpan.h (revision 72a5e6bb)
1cefc8c8aSAlexander Aring /*
2cefc8c8aSAlexander Aring  * Copyright 2011, Siemens AG
3cefc8c8aSAlexander Aring  * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
4cefc8c8aSAlexander Aring  */
5cefc8c8aSAlexander Aring 
6cefc8c8aSAlexander Aring /*
7cefc8c8aSAlexander Aring  * Based on patches from Jon Smirl <jonsmirl@gmail.com>
8cefc8c8aSAlexander Aring  * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
9cefc8c8aSAlexander Aring  *
10cefc8c8aSAlexander Aring  * This program is free software; you can redistribute it and/or modify
11cefc8c8aSAlexander Aring  * it under the terms of the GNU General Public License version 2
12cefc8c8aSAlexander Aring  * as published by the Free Software Foundation.
13cefc8c8aSAlexander Aring  *
14cefc8c8aSAlexander Aring  * This program is distributed in the hope that it will be useful,
15cefc8c8aSAlexander Aring  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16cefc8c8aSAlexander Aring  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17cefc8c8aSAlexander Aring  * GNU General Public License for more details.
18cefc8c8aSAlexander Aring  *
19cefc8c8aSAlexander Aring  * You should have received a copy of the GNU General Public License along
20cefc8c8aSAlexander Aring  * with this program; if not, write to the Free Software Foundation, Inc.,
21cefc8c8aSAlexander Aring  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22cefc8c8aSAlexander Aring  */
23cefc8c8aSAlexander Aring 
24cefc8c8aSAlexander Aring /* Jon's code is based on 6lowpan implementation for Contiki which is:
25cefc8c8aSAlexander Aring  * Copyright (c) 2008, Swedish Institute of Computer Science.
26cefc8c8aSAlexander Aring  * All rights reserved.
27cefc8c8aSAlexander Aring  *
28cefc8c8aSAlexander Aring  * Redistribution and use in source and binary forms, with or without
29cefc8c8aSAlexander Aring  * modification, are permitted provided that the following conditions
30cefc8c8aSAlexander Aring  * are met:
31cefc8c8aSAlexander Aring  * 1. Redistributions of source code must retain the above copyright
32cefc8c8aSAlexander Aring  *    notice, this list of conditions and the following disclaimer.
33cefc8c8aSAlexander Aring  * 2. Redistributions in binary form must reproduce the above copyright
34cefc8c8aSAlexander Aring  *    notice, this list of conditions and the following disclaimer in the
35cefc8c8aSAlexander Aring  *    documentation and/or other materials provided with the distribution.
36cefc8c8aSAlexander Aring  * 3. Neither the name of the Institute nor the names of its contributors
37cefc8c8aSAlexander Aring  *    may be used to endorse or promote products derived from this software
38cefc8c8aSAlexander Aring  *    without specific prior written permission.
39cefc8c8aSAlexander Aring  *
40cefc8c8aSAlexander Aring  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
41cefc8c8aSAlexander Aring  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42cefc8c8aSAlexander Aring  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43cefc8c8aSAlexander Aring  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
44cefc8c8aSAlexander Aring  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45cefc8c8aSAlexander Aring  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46cefc8c8aSAlexander Aring  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47cefc8c8aSAlexander Aring  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48cefc8c8aSAlexander Aring  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49cefc8c8aSAlexander Aring  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50cefc8c8aSAlexander Aring  * SUCH DAMAGE.
51cefc8c8aSAlexander Aring  */
52cefc8c8aSAlexander Aring 
53cefc8c8aSAlexander Aring #ifndef __6LOWPAN_H__
54cefc8c8aSAlexander Aring #define __6LOWPAN_H__
55cefc8c8aSAlexander Aring 
56cefc8c8aSAlexander Aring #include <net/ipv6.h>
5717d8ecb8SLuis R. Rodriguez #include <net/net_namespace.h>
58cefc8c8aSAlexander Aring 
59cefc8c8aSAlexander Aring #define UIP_802154_SHORTADDR_LEN	2  /* compressed ipv6 address length */
60cefc8c8aSAlexander Aring #define UIP_IPH_LEN			40 /* ipv6 fixed header size */
61cefc8c8aSAlexander Aring #define UIP_PROTO_UDP			17 /* ipv6 next header value for UDP */
62cefc8c8aSAlexander Aring #define UIP_FRAGH_LEN			8  /* ipv6 fragment header size */
63cefc8c8aSAlexander Aring 
64cefc8c8aSAlexander Aring /*
65cefc8c8aSAlexander Aring  * ipv6 address based on mac
66cefc8c8aSAlexander Aring  * second bit-flip (Universe/Local) is done according RFC2464
67cefc8c8aSAlexander Aring  */
68cefc8c8aSAlexander Aring #define is_addr_mac_addr_based(a, m) \
69cefc8c8aSAlexander Aring 	((((a)->s6_addr[8])  == (((m)[0]) ^ 0x02)) &&	\
70cefc8c8aSAlexander Aring 	 (((a)->s6_addr[9])  == (m)[1]) &&		\
71cefc8c8aSAlexander Aring 	 (((a)->s6_addr[10]) == (m)[2]) &&		\
72cefc8c8aSAlexander Aring 	 (((a)->s6_addr[11]) == (m)[3]) &&		\
73cefc8c8aSAlexander Aring 	 (((a)->s6_addr[12]) == (m)[4]) &&		\
74cefc8c8aSAlexander Aring 	 (((a)->s6_addr[13]) == (m)[5]) &&		\
75cefc8c8aSAlexander Aring 	 (((a)->s6_addr[14]) == (m)[6]) &&		\
76cefc8c8aSAlexander Aring 	 (((a)->s6_addr[15]) == (m)[7]))
77cefc8c8aSAlexander Aring 
78cefc8c8aSAlexander Aring /*
79cefc8c8aSAlexander Aring  * check whether we can compress the IID to 16 bits,
80cefc8c8aSAlexander Aring  * it's possible for unicast adresses with first 49 bits are zero only.
81cefc8c8aSAlexander Aring  */
82cefc8c8aSAlexander Aring #define lowpan_is_iid_16_bit_compressable(a)	\
83cefc8c8aSAlexander Aring 	((((a)->s6_addr16[4]) == 0) &&		\
84cefc8c8aSAlexander Aring 	 (((a)->s6_addr[10]) == 0) &&		\
85cefc8c8aSAlexander Aring 	 (((a)->s6_addr[11]) == 0xff) &&	\
86cefc8c8aSAlexander Aring 	 (((a)->s6_addr[12]) == 0xfe) &&	\
87cefc8c8aSAlexander Aring 	 (((a)->s6_addr[13]) == 0))
88cefc8c8aSAlexander Aring 
89cefc8c8aSAlexander Aring /* check whether the 112-bit gid of the multicast address is mappable to: */
90cefc8c8aSAlexander Aring 
91cefc8c8aSAlexander Aring /* 48 bits, FFXX::00XX:XXXX:XXXX */
92cefc8c8aSAlexander Aring #define lowpan_is_mcast_addr_compressable48(a)	\
93cefc8c8aSAlexander Aring 	((((a)->s6_addr16[1]) == 0) &&		\
94cefc8c8aSAlexander Aring 	 (((a)->s6_addr16[2]) == 0) &&		\
95cefc8c8aSAlexander Aring 	 (((a)->s6_addr16[3]) == 0) &&		\
96cefc8c8aSAlexander Aring 	 (((a)->s6_addr16[4]) == 0) &&		\
97cefc8c8aSAlexander Aring 	 (((a)->s6_addr[10]) == 0))
98cefc8c8aSAlexander Aring 
99cefc8c8aSAlexander Aring /* 32 bits, FFXX::00XX:XXXX */
100cefc8c8aSAlexander Aring #define lowpan_is_mcast_addr_compressable32(a)	\
101cefc8c8aSAlexander Aring 	((((a)->s6_addr16[1]) == 0) &&		\
102cefc8c8aSAlexander Aring 	 (((a)->s6_addr16[2]) == 0) &&		\
103cefc8c8aSAlexander Aring 	 (((a)->s6_addr16[3]) == 0) &&		\
104cefc8c8aSAlexander Aring 	 (((a)->s6_addr16[4]) == 0) &&		\
105cefc8c8aSAlexander Aring 	 (((a)->s6_addr16[5]) == 0) &&		\
106cefc8c8aSAlexander Aring 	 (((a)->s6_addr[12]) == 0))
107cefc8c8aSAlexander Aring 
108cefc8c8aSAlexander Aring /* 8 bits, FF02::00XX */
109cefc8c8aSAlexander Aring #define lowpan_is_mcast_addr_compressable8(a)	\
110cefc8c8aSAlexander Aring 	((((a)->s6_addr[1])  == 2) &&		\
111cefc8c8aSAlexander Aring 	 (((a)->s6_addr16[1]) == 0) &&		\
112cefc8c8aSAlexander Aring 	 (((a)->s6_addr16[2]) == 0) &&		\
113cefc8c8aSAlexander Aring 	 (((a)->s6_addr16[3]) == 0) &&		\
114cefc8c8aSAlexander Aring 	 (((a)->s6_addr16[4]) == 0) &&		\
115cefc8c8aSAlexander Aring 	 (((a)->s6_addr16[5]) == 0) &&		\
116cefc8c8aSAlexander Aring 	 (((a)->s6_addr16[6]) == 0) &&		\
117cefc8c8aSAlexander Aring 	 (((a)->s6_addr[14]) == 0))
118cefc8c8aSAlexander Aring 
119cefc8c8aSAlexander Aring #define lowpan_is_addr_broadcast(a)	\
120cefc8c8aSAlexander Aring 	((((a)[0]) == 0xFF) &&	\
121cefc8c8aSAlexander Aring 	 (((a)[1]) == 0xFF) &&	\
122cefc8c8aSAlexander Aring 	 (((a)[2]) == 0xFF) &&	\
123cefc8c8aSAlexander Aring 	 (((a)[3]) == 0xFF) &&	\
124cefc8c8aSAlexander Aring 	 (((a)[4]) == 0xFF) &&	\
125cefc8c8aSAlexander Aring 	 (((a)[5]) == 0xFF) &&	\
126cefc8c8aSAlexander Aring 	 (((a)[6]) == 0xFF) &&	\
127cefc8c8aSAlexander Aring 	 (((a)[7]) == 0xFF))
128cefc8c8aSAlexander Aring 
129cefc8c8aSAlexander Aring #define LOWPAN_DISPATCH_IPV6		0x41 /* 01000001 = 65 */
130cefc8c8aSAlexander Aring #define LOWPAN_DISPATCH_IPHC		0x60 /* 011xxxxx = ... */
13172a5e6bbSAlexander Aring #define LOWPAN_DISPATCH_IPHC_MASK	0xe0
132cefc8c8aSAlexander Aring 
13372a5e6bbSAlexander Aring static inline bool lowpan_is_ipv6(u8 dispatch)
13472a5e6bbSAlexander Aring {
13572a5e6bbSAlexander Aring 	return dispatch == LOWPAN_DISPATCH_IPV6;
13672a5e6bbSAlexander Aring }
13772a5e6bbSAlexander Aring 
13872a5e6bbSAlexander Aring static inline bool lowpan_is_iphc(u8 dispatch)
13972a5e6bbSAlexander Aring {
14072a5e6bbSAlexander Aring 	return (dispatch & LOWPAN_DISPATCH_IPHC_MASK) == LOWPAN_DISPATCH_IPHC;
14172a5e6bbSAlexander Aring }
142cefc8c8aSAlexander Aring 
143cefc8c8aSAlexander Aring #define LOWPAN_FRAG_TIMEOUT	(HZ * 60)	/* time-out 60 sec */
144cefc8c8aSAlexander Aring 
145cefc8c8aSAlexander Aring #define LOWPAN_FRAG1_HEAD_SIZE	0x4
146cefc8c8aSAlexander Aring #define LOWPAN_FRAGN_HEAD_SIZE	0x5
147cefc8c8aSAlexander Aring 
148cefc8c8aSAlexander Aring /*
149cefc8c8aSAlexander Aring  * Values of fields within the IPHC encoding first byte
150cefc8c8aSAlexander Aring  * (C stands for compressed and I for inline)
151cefc8c8aSAlexander Aring  */
152cefc8c8aSAlexander Aring #define LOWPAN_IPHC_TF		0x18
153cefc8c8aSAlexander Aring 
154cefc8c8aSAlexander Aring #define LOWPAN_IPHC_FL_C	0x10
155cefc8c8aSAlexander Aring #define LOWPAN_IPHC_TC_C	0x08
156cefc8c8aSAlexander Aring #define LOWPAN_IPHC_NH_C	0x04
157cefc8c8aSAlexander Aring #define LOWPAN_IPHC_TTL_1	0x01
158cefc8c8aSAlexander Aring #define LOWPAN_IPHC_TTL_64	0x02
159cefc8c8aSAlexander Aring #define LOWPAN_IPHC_TTL_255	0x03
160cefc8c8aSAlexander Aring #define LOWPAN_IPHC_TTL_I	0x00
161cefc8c8aSAlexander Aring 
162cefc8c8aSAlexander Aring 
163cefc8c8aSAlexander Aring /* Values of fields within the IPHC encoding second byte */
164cefc8c8aSAlexander Aring #define LOWPAN_IPHC_CID		0x80
165cefc8c8aSAlexander Aring 
166cefc8c8aSAlexander Aring #define LOWPAN_IPHC_ADDR_00	0x00
167cefc8c8aSAlexander Aring #define LOWPAN_IPHC_ADDR_01	0x01
168cefc8c8aSAlexander Aring #define LOWPAN_IPHC_ADDR_02	0x02
169cefc8c8aSAlexander Aring #define LOWPAN_IPHC_ADDR_03	0x03
170cefc8c8aSAlexander Aring 
171cefc8c8aSAlexander Aring #define LOWPAN_IPHC_SAC		0x40
172cefc8c8aSAlexander Aring #define LOWPAN_IPHC_SAM		0x30
173cefc8c8aSAlexander Aring 
174cefc8c8aSAlexander Aring #define LOWPAN_IPHC_SAM_BIT	4
175cefc8c8aSAlexander Aring 
176cefc8c8aSAlexander Aring #define LOWPAN_IPHC_M		0x08
177cefc8c8aSAlexander Aring #define LOWPAN_IPHC_DAC		0x04
178cefc8c8aSAlexander Aring #define LOWPAN_IPHC_DAM_00	0x00
179cefc8c8aSAlexander Aring #define LOWPAN_IPHC_DAM_01	0x01
180cefc8c8aSAlexander Aring #define LOWPAN_IPHC_DAM_10	0x02
181cefc8c8aSAlexander Aring #define LOWPAN_IPHC_DAM_11	0x03
182cefc8c8aSAlexander Aring 
183cefc8c8aSAlexander Aring #define LOWPAN_IPHC_DAM_BIT	0
184cefc8c8aSAlexander Aring /*
185cefc8c8aSAlexander Aring  * LOWPAN_UDP encoding (works together with IPHC)
186cefc8c8aSAlexander Aring  */
187cefc8c8aSAlexander Aring #define LOWPAN_NHC_UDP_MASK		0xF8
188cefc8c8aSAlexander Aring #define LOWPAN_NHC_UDP_ID		0xF0
189cefc8c8aSAlexander Aring #define LOWPAN_NHC_UDP_CHECKSUMC	0x04
190cefc8c8aSAlexander Aring #define LOWPAN_NHC_UDP_CHECKSUMI	0x00
191cefc8c8aSAlexander Aring 
192cefc8c8aSAlexander Aring #define LOWPAN_NHC_UDP_4BIT_PORT	0xF0B0
193cefc8c8aSAlexander Aring #define LOWPAN_NHC_UDP_4BIT_MASK	0xFFF0
194cefc8c8aSAlexander Aring #define LOWPAN_NHC_UDP_8BIT_PORT	0xF000
195cefc8c8aSAlexander Aring #define LOWPAN_NHC_UDP_8BIT_MASK	0xFF00
196cefc8c8aSAlexander Aring 
197cefc8c8aSAlexander Aring /* values for port compression, _with checksum_ ie bit 5 set to 0 */
198cefc8c8aSAlexander Aring #define LOWPAN_NHC_UDP_CS_P_00	0xF0 /* all inline */
199cefc8c8aSAlexander Aring #define LOWPAN_NHC_UDP_CS_P_01	0xF1 /* source 16bit inline,
200cefc8c8aSAlexander Aring 					dest = 0xF0 + 8 bit inline */
201cefc8c8aSAlexander Aring #define LOWPAN_NHC_UDP_CS_P_10	0xF2 /* source = 0xF0 + 8bit inline,
202cefc8c8aSAlexander Aring 					dest = 16 bit inline */
203cefc8c8aSAlexander Aring #define LOWPAN_NHC_UDP_CS_P_11	0xF3 /* source & dest = 0xF0B + 4bit inline */
204cefc8c8aSAlexander Aring #define LOWPAN_NHC_UDP_CS_C	0x04 /* checksum elided */
205cefc8c8aSAlexander Aring 
206b72f6f51SAlexander Aring #define LOWPAN_PRIV_SIZE(llpriv_size)	\
207b72f6f51SAlexander Aring 	(sizeof(struct lowpan_priv) + llpriv_size)
208b72f6f51SAlexander Aring 
209b72f6f51SAlexander Aring enum lowpan_lltypes {
210b72f6f51SAlexander Aring 	LOWPAN_LLTYPE_BTLE,
211b72f6f51SAlexander Aring 	LOWPAN_LLTYPE_IEEE802154,
212b72f6f51SAlexander Aring };
213b72f6f51SAlexander Aring 
214b72f6f51SAlexander Aring struct lowpan_priv {
215b72f6f51SAlexander Aring 	enum lowpan_lltypes lltype;
216b72f6f51SAlexander Aring 
217b72f6f51SAlexander Aring 	/* must be last */
218b72f6f51SAlexander Aring 	u8 priv[0] __aligned(sizeof(void *));
219b72f6f51SAlexander Aring };
220b72f6f51SAlexander Aring 
221b72f6f51SAlexander Aring static inline
222b72f6f51SAlexander Aring struct lowpan_priv *lowpan_priv(const struct net_device *dev)
223b72f6f51SAlexander Aring {
224b72f6f51SAlexander Aring 	return netdev_priv(dev);
225b72f6f51SAlexander Aring }
226b72f6f51SAlexander Aring 
22772a5e6bbSAlexander Aring struct lowpan_802154_cb {
22872a5e6bbSAlexander Aring 	u16 d_tag;
22972a5e6bbSAlexander Aring 	unsigned int d_size;
23072a5e6bbSAlexander Aring 	u8 d_offset;
23172a5e6bbSAlexander Aring };
23272a5e6bbSAlexander Aring 
23372a5e6bbSAlexander Aring static inline
23472a5e6bbSAlexander Aring struct lowpan_802154_cb *lowpan_802154_cb(const struct sk_buff *skb)
23572a5e6bbSAlexander Aring {
23672a5e6bbSAlexander Aring 	BUILD_BUG_ON(sizeof(struct lowpan_802154_cb) > sizeof(skb->cb));
23772a5e6bbSAlexander Aring 	return (struct lowpan_802154_cb *)skb->cb;
23872a5e6bbSAlexander Aring }
23972a5e6bbSAlexander Aring 
240cefc8c8aSAlexander Aring #ifdef DEBUG
241cefc8c8aSAlexander Aring /* print data in line */
242cefc8c8aSAlexander Aring static inline void raw_dump_inline(const char *caller, char *msg,
243cefc8c8aSAlexander Aring 				   unsigned char *buf, int len)
244cefc8c8aSAlexander Aring {
245cefc8c8aSAlexander Aring 	if (msg)
246cefc8c8aSAlexander Aring 		pr_debug("%s():%s: ", caller, msg);
247cefc8c8aSAlexander Aring 
248cefc8c8aSAlexander Aring 	print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, buf, len, false);
249cefc8c8aSAlexander Aring }
250cefc8c8aSAlexander Aring 
251cefc8c8aSAlexander Aring /* print data in a table format:
252cefc8c8aSAlexander Aring  *
253cefc8c8aSAlexander Aring  * addr: xx xx xx xx xx xx
254cefc8c8aSAlexander Aring  * addr: xx xx xx xx xx xx
255cefc8c8aSAlexander Aring  * ...
256cefc8c8aSAlexander Aring  */
257cefc8c8aSAlexander Aring static inline void raw_dump_table(const char *caller, char *msg,
258cefc8c8aSAlexander Aring 				  unsigned char *buf, int len)
259cefc8c8aSAlexander Aring {
260cefc8c8aSAlexander Aring 	if (msg)
261cefc8c8aSAlexander Aring 		pr_debug("%s():%s:\n", caller, msg);
262cefc8c8aSAlexander Aring 
263cefc8c8aSAlexander Aring 	print_hex_dump_debug("\t", DUMP_PREFIX_OFFSET, 16, 1, buf, len, false);
264cefc8c8aSAlexander Aring }
265cefc8c8aSAlexander Aring #else
266cefc8c8aSAlexander Aring static inline void raw_dump_table(const char *caller, char *msg,
267cefc8c8aSAlexander Aring 				  unsigned char *buf, int len) { }
268cefc8c8aSAlexander Aring static inline void raw_dump_inline(const char *caller, char *msg,
269cefc8c8aSAlexander Aring 				   unsigned char *buf, int len) { }
270cefc8c8aSAlexander Aring #endif
271cefc8c8aSAlexander Aring 
272cefc8c8aSAlexander Aring static inline int lowpan_fetch_skb_u8(struct sk_buff *skb, u8 *val)
273cefc8c8aSAlexander Aring {
274cefc8c8aSAlexander Aring 	if (unlikely(!pskb_may_pull(skb, 1)))
275cefc8c8aSAlexander Aring 		return -EINVAL;
276cefc8c8aSAlexander Aring 
277cefc8c8aSAlexander Aring 	*val = skb->data[0];
278cefc8c8aSAlexander Aring 	skb_pull(skb, 1);
279cefc8c8aSAlexander Aring 
280cefc8c8aSAlexander Aring 	return 0;
281cefc8c8aSAlexander Aring }
282cefc8c8aSAlexander Aring 
283cefc8c8aSAlexander Aring static inline bool lowpan_fetch_skb(struct sk_buff *skb,
284cefc8c8aSAlexander Aring 		void *data, const unsigned int len)
285cefc8c8aSAlexander Aring {
286cefc8c8aSAlexander Aring 	if (unlikely(!pskb_may_pull(skb, len)))
287cefc8c8aSAlexander Aring 		return true;
288cefc8c8aSAlexander Aring 
289cefc8c8aSAlexander Aring 	skb_copy_from_linear_data(skb, data, len);
290cefc8c8aSAlexander Aring 	skb_pull(skb, len);
291cefc8c8aSAlexander Aring 
292cefc8c8aSAlexander Aring 	return false;
293cefc8c8aSAlexander Aring }
294cefc8c8aSAlexander Aring 
295cefc8c8aSAlexander Aring static inline void lowpan_push_hc_data(u8 **hc_ptr, const void *data,
296cefc8c8aSAlexander Aring 				       const size_t len)
297cefc8c8aSAlexander Aring {
298cefc8c8aSAlexander Aring 	memcpy(*hc_ptr, data, len);
299cefc8c8aSAlexander Aring 	*hc_ptr += len;
300cefc8c8aSAlexander Aring }
301cefc8c8aSAlexander Aring 
302cefc8c8aSAlexander Aring static inline u8 lowpan_addr_mode_size(const u8 addr_mode)
303cefc8c8aSAlexander Aring {
304cefc8c8aSAlexander Aring 	static const u8 addr_sizes[] = {
305cefc8c8aSAlexander Aring 		[LOWPAN_IPHC_ADDR_00] = 16,
306cefc8c8aSAlexander Aring 		[LOWPAN_IPHC_ADDR_01] = 8,
307cefc8c8aSAlexander Aring 		[LOWPAN_IPHC_ADDR_02] = 2,
308cefc8c8aSAlexander Aring 		[LOWPAN_IPHC_ADDR_03] = 0,
309cefc8c8aSAlexander Aring 	};
310cefc8c8aSAlexander Aring 	return addr_sizes[addr_mode];
311cefc8c8aSAlexander Aring }
312cefc8c8aSAlexander Aring 
313cefc8c8aSAlexander Aring static inline u8 lowpan_next_hdr_size(const u8 h_enc, u16 *uncomp_header)
314cefc8c8aSAlexander Aring {
315cefc8c8aSAlexander Aring 	u8 ret = 1;
316cefc8c8aSAlexander Aring 
317cefc8c8aSAlexander Aring 	if ((h_enc & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
318cefc8c8aSAlexander Aring 		*uncomp_header += sizeof(struct udphdr);
319cefc8c8aSAlexander Aring 
320cefc8c8aSAlexander Aring 		switch (h_enc & LOWPAN_NHC_UDP_CS_P_11) {
321cefc8c8aSAlexander Aring 		case LOWPAN_NHC_UDP_CS_P_00:
322cefc8c8aSAlexander Aring 			ret += 4;
323cefc8c8aSAlexander Aring 			break;
324cefc8c8aSAlexander Aring 		case LOWPAN_NHC_UDP_CS_P_01:
325cefc8c8aSAlexander Aring 		case LOWPAN_NHC_UDP_CS_P_10:
326cefc8c8aSAlexander Aring 			ret += 3;
327cefc8c8aSAlexander Aring 			break;
328cefc8c8aSAlexander Aring 		case LOWPAN_NHC_UDP_CS_P_11:
329cefc8c8aSAlexander Aring 			ret++;
330cefc8c8aSAlexander Aring 			break;
331cefc8c8aSAlexander Aring 		default:
332cefc8c8aSAlexander Aring 			break;
333cefc8c8aSAlexander Aring 		}
334cefc8c8aSAlexander Aring 
335cefc8c8aSAlexander Aring 		if (!(h_enc & LOWPAN_NHC_UDP_CS_C))
336cefc8c8aSAlexander Aring 			ret += 2;
337cefc8c8aSAlexander Aring 	}
338cefc8c8aSAlexander Aring 
339cefc8c8aSAlexander Aring 	return ret;
340cefc8c8aSAlexander Aring }
341cefc8c8aSAlexander Aring 
342cefc8c8aSAlexander Aring /**
343cefc8c8aSAlexander Aring  *	lowpan_uncompress_size - returns skb->len size with uncompressed header
344cefc8c8aSAlexander Aring  *	@skb: sk_buff with 6lowpan header inside
345cefc8c8aSAlexander Aring  *	@datagram_offset: optional to get the datagram_offset value
346cefc8c8aSAlexander Aring  *
347cefc8c8aSAlexander Aring  *	Returns the skb->len with uncompressed header
348cefc8c8aSAlexander Aring  */
349cefc8c8aSAlexander Aring static inline u16
350cefc8c8aSAlexander Aring lowpan_uncompress_size(const struct sk_buff *skb, u16 *dgram_offset)
351cefc8c8aSAlexander Aring {
352cefc8c8aSAlexander Aring 	u16 ret = 2, uncomp_header = sizeof(struct ipv6hdr);
353cefc8c8aSAlexander Aring 	u8 iphc0, iphc1, h_enc;
354cefc8c8aSAlexander Aring 
355cefc8c8aSAlexander Aring 	iphc0 = skb_network_header(skb)[0];
356cefc8c8aSAlexander Aring 	iphc1 = skb_network_header(skb)[1];
357cefc8c8aSAlexander Aring 
358cefc8c8aSAlexander Aring 	switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
359cefc8c8aSAlexander Aring 	case 0:
360cefc8c8aSAlexander Aring 		ret += 4;
361cefc8c8aSAlexander Aring 		break;
362cefc8c8aSAlexander Aring 	case 1:
363cefc8c8aSAlexander Aring 		ret += 3;
364cefc8c8aSAlexander Aring 		break;
365cefc8c8aSAlexander Aring 	case 2:
366cefc8c8aSAlexander Aring 		ret++;
367cefc8c8aSAlexander Aring 		break;
368cefc8c8aSAlexander Aring 	default:
369cefc8c8aSAlexander Aring 		break;
370cefc8c8aSAlexander Aring 	}
371cefc8c8aSAlexander Aring 
372cefc8c8aSAlexander Aring 	if (!(iphc0 & LOWPAN_IPHC_NH_C))
373cefc8c8aSAlexander Aring 		ret++;
374cefc8c8aSAlexander Aring 
375cefc8c8aSAlexander Aring 	if (!(iphc0 & 0x03))
376cefc8c8aSAlexander Aring 		ret++;
377cefc8c8aSAlexander Aring 
378cefc8c8aSAlexander Aring 	ret += lowpan_addr_mode_size((iphc1 & LOWPAN_IPHC_SAM) >>
379cefc8c8aSAlexander Aring 				     LOWPAN_IPHC_SAM_BIT);
380cefc8c8aSAlexander Aring 
381cefc8c8aSAlexander Aring 	if (iphc1 & LOWPAN_IPHC_M) {
382cefc8c8aSAlexander Aring 		switch ((iphc1 & LOWPAN_IPHC_DAM_11) >>
383cefc8c8aSAlexander Aring 			LOWPAN_IPHC_DAM_BIT) {
384cefc8c8aSAlexander Aring 		case LOWPAN_IPHC_DAM_00:
385cefc8c8aSAlexander Aring 			ret += 16;
386cefc8c8aSAlexander Aring 			break;
387cefc8c8aSAlexander Aring 		case LOWPAN_IPHC_DAM_01:
388cefc8c8aSAlexander Aring 			ret += 6;
389cefc8c8aSAlexander Aring 			break;
390cefc8c8aSAlexander Aring 		case LOWPAN_IPHC_DAM_10:
391cefc8c8aSAlexander Aring 			ret += 4;
392cefc8c8aSAlexander Aring 			break;
393cefc8c8aSAlexander Aring 		case LOWPAN_IPHC_DAM_11:
394cefc8c8aSAlexander Aring 			ret++;
395cefc8c8aSAlexander Aring 			break;
396cefc8c8aSAlexander Aring 		default:
397cefc8c8aSAlexander Aring 			break;
398cefc8c8aSAlexander Aring 		}
399cefc8c8aSAlexander Aring 	} else {
400cefc8c8aSAlexander Aring 		ret += lowpan_addr_mode_size((iphc1 & LOWPAN_IPHC_DAM_11) >>
401cefc8c8aSAlexander Aring 					     LOWPAN_IPHC_DAM_BIT);
402cefc8c8aSAlexander Aring 	}
403cefc8c8aSAlexander Aring 
404cefc8c8aSAlexander Aring 	if (iphc0 & LOWPAN_IPHC_NH_C) {
405cefc8c8aSAlexander Aring 		h_enc = skb_network_header(skb)[ret];
406cefc8c8aSAlexander Aring 		ret += lowpan_next_hdr_size(h_enc, &uncomp_header);
407cefc8c8aSAlexander Aring 	}
408cefc8c8aSAlexander Aring 
409cefc8c8aSAlexander Aring 	if (dgram_offset)
410cefc8c8aSAlexander Aring 		*dgram_offset = uncomp_header;
411cefc8c8aSAlexander Aring 
412cefc8c8aSAlexander Aring 	return skb->len + uncomp_header - ret;
413cefc8c8aSAlexander Aring }
414cefc8c8aSAlexander Aring 
415b72f6f51SAlexander Aring void lowpan_netdev_setup(struct net_device *dev, enum lowpan_lltypes lltype);
416b72f6f51SAlexander Aring 
41701141234SMartin Townsend int
41801141234SMartin Townsend lowpan_header_decompress(struct sk_buff *skb, struct net_device *dev,
41901141234SMartin Townsend 			 const u8 *saddr, const u8 saddr_type,
42001141234SMartin Townsend 			 const u8 saddr_len, const u8 *daddr,
42101141234SMartin Townsend 			 const u8 daddr_type, const u8 daddr_len,
422f8b36176SMartin Townsend 			 u8 iphc0, u8 iphc1);
423cefc8c8aSAlexander Aring int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
424cefc8c8aSAlexander Aring 			unsigned short type, const void *_daddr,
425cefc8c8aSAlexander Aring 			const void *_saddr, unsigned int len);
426cefc8c8aSAlexander Aring 
427cefc8c8aSAlexander Aring #endif /* __6LOWPAN_H__ */
428