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 56b1815fd9SAlexander Aring #include <linux/debugfs.h> 57b1815fd9SAlexander Aring 58cefc8c8aSAlexander Aring #include <net/ipv6.h> 5917d8ecb8SLuis R. Rodriguez #include <net/net_namespace.h> 60cefc8c8aSAlexander Aring 61edc73417SAlexander Aring /* special link-layer handling */ 62edc73417SAlexander Aring #include <net/mac802154.h> 63edc73417SAlexander Aring 644d6a6aedSAlexander Aring #define EUI64_ADDR_LEN 8 654d6a6aedSAlexander Aring 6687a93e4eSAlexander Aring #define LOWPAN_NHC_MAX_ID_LEN 1 67bf513fd6SAlexander Aring /* Maximum next header compression length which we currently support inclusive 68bf513fd6SAlexander Aring * possible inline data. 69bf513fd6SAlexander Aring */ 70bf513fd6SAlexander Aring #define LOWPAN_NHC_MAX_HDR_LEN (sizeof(struct udphdr)) 7187a93e4eSAlexander Aring /* Max IPHC Header len without IPv6 hdr specific inline data. 7287a93e4eSAlexander Aring * Useful for getting the "extra" bytes we need at worst case compression. 7387a93e4eSAlexander Aring * 7487a93e4eSAlexander Aring * LOWPAN_IPHC + CID + LOWPAN_NHC_MAX_ID_LEN 7587a93e4eSAlexander Aring */ 7687a93e4eSAlexander Aring #define LOWPAN_IPHC_MAX_HEADER_LEN (2 + 1 + LOWPAN_NHC_MAX_ID_LEN) 77bf513fd6SAlexander Aring /* Maximum worst case IPHC header buffer size */ 78bf513fd6SAlexander Aring #define LOWPAN_IPHC_MAX_HC_BUF_LEN (sizeof(struct ipv6hdr) + \ 79bf513fd6SAlexander Aring LOWPAN_IPHC_MAX_HEADER_LEN + \ 80bf513fd6SAlexander Aring LOWPAN_NHC_MAX_HDR_LEN) 815609c185SAlexander Aring /* SCI/DCI is 4 bit width, so we have maximum 16 entries */ 825609c185SAlexander Aring #define LOWPAN_IPHC_CTX_TABLE_SIZE (1 << 4) 8387a93e4eSAlexander Aring 84cefc8c8aSAlexander Aring #define LOWPAN_DISPATCH_IPV6 0x41 /* 01000001 = 65 */ 85cefc8c8aSAlexander Aring #define LOWPAN_DISPATCH_IPHC 0x60 /* 011xxxxx = ... */ 8672a5e6bbSAlexander Aring #define LOWPAN_DISPATCH_IPHC_MASK 0xe0 87cefc8c8aSAlexander Aring 8872a5e6bbSAlexander Aring static inline bool lowpan_is_ipv6(u8 dispatch) 8972a5e6bbSAlexander Aring { 9072a5e6bbSAlexander Aring return dispatch == LOWPAN_DISPATCH_IPV6; 9172a5e6bbSAlexander Aring } 9272a5e6bbSAlexander Aring 9372a5e6bbSAlexander Aring static inline bool lowpan_is_iphc(u8 dispatch) 9472a5e6bbSAlexander Aring { 9572a5e6bbSAlexander Aring return (dispatch & LOWPAN_DISPATCH_IPHC_MASK) == LOWPAN_DISPATCH_IPHC; 9672a5e6bbSAlexander Aring } 97cefc8c8aSAlexander Aring 98b72f6f51SAlexander Aring #define LOWPAN_PRIV_SIZE(llpriv_size) \ 992e4d60cbSAlexander Aring (sizeof(struct lowpan_dev) + llpriv_size) 100b72f6f51SAlexander Aring 101b72f6f51SAlexander Aring enum lowpan_lltypes { 102b72f6f51SAlexander Aring LOWPAN_LLTYPE_BTLE, 103b72f6f51SAlexander Aring LOWPAN_LLTYPE_IEEE802154, 104b72f6f51SAlexander Aring }; 105b72f6f51SAlexander Aring 1065609c185SAlexander Aring enum lowpan_iphc_ctx_flags { 1075609c185SAlexander Aring LOWPAN_IPHC_CTX_FLAG_ACTIVE, 1085609c185SAlexander Aring LOWPAN_IPHC_CTX_FLAG_COMPRESSION, 1095609c185SAlexander Aring }; 1105609c185SAlexander Aring 1115609c185SAlexander Aring struct lowpan_iphc_ctx { 1125609c185SAlexander Aring u8 id; 1135609c185SAlexander Aring struct in6_addr pfx; 1145609c185SAlexander Aring u8 plen; 1155609c185SAlexander Aring unsigned long flags; 1165609c185SAlexander Aring }; 1175609c185SAlexander Aring 1185609c185SAlexander Aring struct lowpan_iphc_ctx_table { 1195609c185SAlexander Aring spinlock_t lock; 1205609c185SAlexander Aring const struct lowpan_iphc_ctx_ops *ops; 1215609c185SAlexander Aring struct lowpan_iphc_ctx table[LOWPAN_IPHC_CTX_TABLE_SIZE]; 1225609c185SAlexander Aring }; 1235609c185SAlexander Aring 1245609c185SAlexander Aring static inline bool lowpan_iphc_ctx_is_active(const struct lowpan_iphc_ctx *ctx) 1255609c185SAlexander Aring { 1265609c185SAlexander Aring return test_bit(LOWPAN_IPHC_CTX_FLAG_ACTIVE, &ctx->flags); 1275609c185SAlexander Aring } 1285609c185SAlexander Aring 1295609c185SAlexander Aring static inline bool 1305609c185SAlexander Aring lowpan_iphc_ctx_is_compression(const struct lowpan_iphc_ctx *ctx) 1315609c185SAlexander Aring { 1325609c185SAlexander Aring return test_bit(LOWPAN_IPHC_CTX_FLAG_COMPRESSION, &ctx->flags); 1335609c185SAlexander Aring } 1345609c185SAlexander Aring 1352e4d60cbSAlexander Aring struct lowpan_dev { 136b72f6f51SAlexander Aring enum lowpan_lltypes lltype; 137b1815fd9SAlexander Aring struct dentry *iface_debugfs; 1385609c185SAlexander Aring struct lowpan_iphc_ctx_table ctx; 139b72f6f51SAlexander Aring 140b72f6f51SAlexander Aring /* must be last */ 141b72f6f51SAlexander Aring u8 priv[0] __aligned(sizeof(void *)); 142b72f6f51SAlexander Aring }; 143b72f6f51SAlexander Aring 144*8626a0c8SAlexander Aring struct lowpan_802154_neigh { 145*8626a0c8SAlexander Aring __le16 short_addr; 146*8626a0c8SAlexander Aring }; 147*8626a0c8SAlexander Aring 148*8626a0c8SAlexander Aring static inline 149*8626a0c8SAlexander Aring struct lowpan_802154_neigh *lowpan_802154_neigh(void *neigh_priv) 150*8626a0c8SAlexander Aring { 151*8626a0c8SAlexander Aring return neigh_priv; 152*8626a0c8SAlexander Aring } 153*8626a0c8SAlexander Aring 154b72f6f51SAlexander Aring static inline 1552e4d60cbSAlexander Aring struct lowpan_dev *lowpan_dev(const struct net_device *dev) 156b72f6f51SAlexander Aring { 157b72f6f51SAlexander Aring return netdev_priv(dev); 158b72f6f51SAlexander Aring } 159b72f6f51SAlexander Aring 160353c224eSAlexander Aring /* private device info */ 161353c224eSAlexander Aring struct lowpan_802154_dev { 162353c224eSAlexander Aring struct net_device *wdev; /* wpan device ptr */ 163353c224eSAlexander Aring u16 fragment_tag; 164353c224eSAlexander Aring }; 165353c224eSAlexander Aring 166353c224eSAlexander Aring static inline struct 167353c224eSAlexander Aring lowpan_802154_dev *lowpan_802154_dev(const struct net_device *dev) 168353c224eSAlexander Aring { 169353c224eSAlexander Aring return (struct lowpan_802154_dev *)lowpan_dev(dev)->priv; 170353c224eSAlexander Aring } 171353c224eSAlexander Aring 17272a5e6bbSAlexander Aring struct lowpan_802154_cb { 17372a5e6bbSAlexander Aring u16 d_tag; 17472a5e6bbSAlexander Aring unsigned int d_size; 17572a5e6bbSAlexander Aring u8 d_offset; 17672a5e6bbSAlexander Aring }; 17772a5e6bbSAlexander Aring 17872a5e6bbSAlexander Aring static inline 17972a5e6bbSAlexander Aring struct lowpan_802154_cb *lowpan_802154_cb(const struct sk_buff *skb) 18072a5e6bbSAlexander Aring { 18172a5e6bbSAlexander Aring BUILD_BUG_ON(sizeof(struct lowpan_802154_cb) > sizeof(skb->cb)); 18272a5e6bbSAlexander Aring return (struct lowpan_802154_cb *)skb->cb; 18372a5e6bbSAlexander Aring } 18472a5e6bbSAlexander Aring 185a5862f2aSAlexander Aring static inline void lowpan_iphc_uncompress_eui64_lladdr(struct in6_addr *ipaddr, 186a5862f2aSAlexander Aring const void *lladdr) 187a5862f2aSAlexander Aring { 188a5862f2aSAlexander Aring /* fe:80::XXXX:XXXX:XXXX:XXXX 189a5862f2aSAlexander Aring * \_________________/ 190a5862f2aSAlexander Aring * hwaddr 191a5862f2aSAlexander Aring */ 192a5862f2aSAlexander Aring ipaddr->s6_addr[0] = 0xFE; 193a5862f2aSAlexander Aring ipaddr->s6_addr[1] = 0x80; 194a5862f2aSAlexander Aring memcpy(&ipaddr->s6_addr[8], lladdr, EUI64_ADDR_LEN); 195a5862f2aSAlexander Aring /* second bit-flip (Universe/Local) 196a5862f2aSAlexander Aring * is done according RFC2464 197a5862f2aSAlexander Aring */ 198a5862f2aSAlexander Aring ipaddr->s6_addr[8] ^= 0x02; 199a5862f2aSAlexander Aring } 200a5862f2aSAlexander Aring 201cefc8c8aSAlexander Aring #ifdef DEBUG 202cefc8c8aSAlexander Aring /* print data in line */ 203cefc8c8aSAlexander Aring static inline void raw_dump_inline(const char *caller, char *msg, 204a6f77389SAlexander Aring const unsigned char *buf, int len) 205cefc8c8aSAlexander Aring { 206cefc8c8aSAlexander Aring if (msg) 207cefc8c8aSAlexander Aring pr_debug("%s():%s: ", caller, msg); 208cefc8c8aSAlexander Aring 209cefc8c8aSAlexander Aring print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, buf, len, false); 210cefc8c8aSAlexander Aring } 211cefc8c8aSAlexander Aring 212cefc8c8aSAlexander Aring /* print data in a table format: 213cefc8c8aSAlexander Aring * 214cefc8c8aSAlexander Aring * addr: xx xx xx xx xx xx 215cefc8c8aSAlexander Aring * addr: xx xx xx xx xx xx 216cefc8c8aSAlexander Aring * ... 217cefc8c8aSAlexander Aring */ 218cefc8c8aSAlexander Aring static inline void raw_dump_table(const char *caller, char *msg, 219a6f77389SAlexander Aring const unsigned char *buf, int len) 220cefc8c8aSAlexander Aring { 221cefc8c8aSAlexander Aring if (msg) 222cefc8c8aSAlexander Aring pr_debug("%s():%s:\n", caller, msg); 223cefc8c8aSAlexander Aring 224cefc8c8aSAlexander Aring print_hex_dump_debug("\t", DUMP_PREFIX_OFFSET, 16, 1, buf, len, false); 225cefc8c8aSAlexander Aring } 226cefc8c8aSAlexander Aring #else 227cefc8c8aSAlexander Aring static inline void raw_dump_table(const char *caller, char *msg, 228a6f77389SAlexander Aring const unsigned char *buf, int len) { } 229cefc8c8aSAlexander Aring static inline void raw_dump_inline(const char *caller, char *msg, 230a6f77389SAlexander Aring const unsigned char *buf, int len) { } 231cefc8c8aSAlexander Aring #endif 232cefc8c8aSAlexander Aring 233478208e3SAlexander Aring /** 234478208e3SAlexander Aring * lowpan_fetch_skb - getting inline data from 6LoWPAN header 235478208e3SAlexander Aring * 236478208e3SAlexander Aring * This function will pull data from sk buffer and put it into data to 237478208e3SAlexander Aring * remove the 6LoWPAN inline data. This function returns true if the 238478208e3SAlexander Aring * sk buffer is too small to pull the amount of data which is specified 239478208e3SAlexander Aring * by len. 240478208e3SAlexander Aring * 241478208e3SAlexander Aring * @skb: the buffer where the inline data should be pulled from. 242478208e3SAlexander Aring * @data: destination buffer for the inline data. 243478208e3SAlexander Aring * @len: amount of data which should be pulled in bytes. 244478208e3SAlexander Aring */ 245478208e3SAlexander Aring static inline bool lowpan_fetch_skb(struct sk_buff *skb, void *data, 246478208e3SAlexander Aring unsigned int len) 247cefc8c8aSAlexander Aring { 248cefc8c8aSAlexander Aring if (unlikely(!pskb_may_pull(skb, len))) 249cefc8c8aSAlexander Aring return true; 250cefc8c8aSAlexander Aring 251cefc8c8aSAlexander Aring skb_copy_from_linear_data(skb, data, len); 252cefc8c8aSAlexander Aring skb_pull(skb, len); 253cefc8c8aSAlexander Aring 254cefc8c8aSAlexander Aring return false; 255cefc8c8aSAlexander Aring } 256cefc8c8aSAlexander Aring 257cefc8c8aSAlexander Aring static inline void lowpan_push_hc_data(u8 **hc_ptr, const void *data, 258cefc8c8aSAlexander Aring const size_t len) 259cefc8c8aSAlexander Aring { 260cefc8c8aSAlexander Aring memcpy(*hc_ptr, data, len); 261cefc8c8aSAlexander Aring *hc_ptr += len; 262cefc8c8aSAlexander Aring } 263cefc8c8aSAlexander Aring 26400f59314SAlexander Aring int lowpan_register_netdevice(struct net_device *dev, 26500f59314SAlexander Aring enum lowpan_lltypes lltype); 26600f59314SAlexander Aring int lowpan_register_netdev(struct net_device *dev, 26700f59314SAlexander Aring enum lowpan_lltypes lltype); 26800f59314SAlexander Aring void lowpan_unregister_netdevice(struct net_device *dev); 26900f59314SAlexander Aring void lowpan_unregister_netdev(struct net_device *dev); 270b72f6f51SAlexander Aring 2718911d774SAlexander Aring /** 2728911d774SAlexander Aring * lowpan_header_decompress - replace 6LoWPAN header with IPv6 header 2738911d774SAlexander Aring * 2748911d774SAlexander Aring * This function replaces the IPHC 6LoWPAN header which should be pointed at 2758911d774SAlexander Aring * skb->data and skb_network_header, with the IPv6 header. 2768911d774SAlexander Aring * It would be nice that the caller have the necessary headroom of IPv6 header 2778911d774SAlexander Aring * and greatest Transport layer header, this would reduce the overhead for 2788911d774SAlexander Aring * reallocate headroom. 2798911d774SAlexander Aring * 2808911d774SAlexander Aring * @skb: the buffer which should be manipulate. 2818911d774SAlexander Aring * @dev: the lowpan net device pointer. 2828911d774SAlexander Aring * @daddr: destination lladdr of mac header which is used for compression 2838911d774SAlexander Aring * methods. 2848911d774SAlexander Aring * @saddr: source lladdr of mac header which is used for compression 2858911d774SAlexander Aring * methods. 2868911d774SAlexander Aring */ 2878911d774SAlexander Aring int lowpan_header_decompress(struct sk_buff *skb, const struct net_device *dev, 2888911d774SAlexander Aring const void *daddr, const void *saddr); 289a6f77389SAlexander Aring 290a6f77389SAlexander Aring /** 291a6f77389SAlexander Aring * lowpan_header_compress - replace IPv6 header with 6LoWPAN header 292a6f77389SAlexander Aring * 293a6f77389SAlexander Aring * This function replaces the IPv6 header which should be pointed at 294a6f77389SAlexander Aring * skb->data and skb_network_header, with the IPHC 6LoWPAN header. 295a6f77389SAlexander Aring * The caller need to be sure that the sk buffer is not shared and at have 296a6f77389SAlexander Aring * at least a headroom which is smaller or equal LOWPAN_IPHC_MAX_HEADER_LEN, 297a6f77389SAlexander Aring * which is the IPHC "more bytes than IPv6 header" at worst case. 298a6f77389SAlexander Aring * 299a6f77389SAlexander Aring * @skb: the buffer which should be manipulate. 300a6f77389SAlexander Aring * @dev: the lowpan net device pointer. 301a6f77389SAlexander Aring * @daddr: destination lladdr of mac header which is used for compression 302a6f77389SAlexander Aring * methods. 303a6f77389SAlexander Aring * @saddr: source lladdr of mac header which is used for compression 304a6f77389SAlexander Aring * methods. 305a6f77389SAlexander Aring */ 306a6f77389SAlexander Aring int lowpan_header_compress(struct sk_buff *skb, const struct net_device *dev, 307a6f77389SAlexander Aring const void *daddr, const void *saddr); 308cefc8c8aSAlexander Aring 309cefc8c8aSAlexander Aring #endif /* __6LOWPAN_H__ */ 310