6lowpan.h 12 KB

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