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 594d6a6aedSAlexander Aring #define EUI64_ADDR_LEN 8 604d6a6aedSAlexander Aring 6187a93e4eSAlexander Aring #define LOWPAN_NHC_MAX_ID_LEN 1 62bf513fd6SAlexander Aring /* Maximum next header compression length which we currently support inclusive 63bf513fd6SAlexander Aring * possible inline data. 64bf513fd6SAlexander Aring */ 65bf513fd6SAlexander Aring #define LOWPAN_NHC_MAX_HDR_LEN (sizeof(struct udphdr)) 6687a93e4eSAlexander Aring /* Max IPHC Header len without IPv6 hdr specific inline data. 6787a93e4eSAlexander Aring * Useful for getting the "extra" bytes we need at worst case compression. 6887a93e4eSAlexander Aring * 6987a93e4eSAlexander Aring * LOWPAN_IPHC + CID + LOWPAN_NHC_MAX_ID_LEN 7087a93e4eSAlexander Aring */ 7187a93e4eSAlexander Aring #define LOWPAN_IPHC_MAX_HEADER_LEN (2 + 1 + LOWPAN_NHC_MAX_ID_LEN) 72bf513fd6SAlexander Aring /* Maximum worst case IPHC header buffer size */ 73bf513fd6SAlexander Aring #define LOWPAN_IPHC_MAX_HC_BUF_LEN (sizeof(struct ipv6hdr) + \ 74bf513fd6SAlexander Aring LOWPAN_IPHC_MAX_HEADER_LEN + \ 75bf513fd6SAlexander Aring LOWPAN_NHC_MAX_HDR_LEN) 7687a93e4eSAlexander Aring 77cefc8c8aSAlexander Aring #define LOWPAN_DISPATCH_IPV6 0x41 /* 01000001 = 65 */ 78cefc8c8aSAlexander Aring #define LOWPAN_DISPATCH_IPHC 0x60 /* 011xxxxx = ... */ 7972a5e6bbSAlexander Aring #define LOWPAN_DISPATCH_IPHC_MASK 0xe0 80cefc8c8aSAlexander Aring 8172a5e6bbSAlexander Aring static inline bool lowpan_is_ipv6(u8 dispatch) 8272a5e6bbSAlexander Aring { 8372a5e6bbSAlexander Aring return dispatch == LOWPAN_DISPATCH_IPV6; 8472a5e6bbSAlexander Aring } 8572a5e6bbSAlexander Aring 8672a5e6bbSAlexander Aring static inline bool lowpan_is_iphc(u8 dispatch) 8772a5e6bbSAlexander Aring { 8872a5e6bbSAlexander Aring return (dispatch & LOWPAN_DISPATCH_IPHC_MASK) == LOWPAN_DISPATCH_IPHC; 8972a5e6bbSAlexander Aring } 90cefc8c8aSAlexander Aring 91b72f6f51SAlexander Aring #define LOWPAN_PRIV_SIZE(llpriv_size) \ 92b72f6f51SAlexander Aring (sizeof(struct lowpan_priv) + llpriv_size) 93b72f6f51SAlexander Aring 94b72f6f51SAlexander Aring enum lowpan_lltypes { 95b72f6f51SAlexander Aring LOWPAN_LLTYPE_BTLE, 96b72f6f51SAlexander Aring LOWPAN_LLTYPE_IEEE802154, 97b72f6f51SAlexander Aring }; 98b72f6f51SAlexander Aring 99b72f6f51SAlexander Aring struct lowpan_priv { 100b72f6f51SAlexander Aring enum lowpan_lltypes lltype; 101b72f6f51SAlexander Aring 102b72f6f51SAlexander Aring /* must be last */ 103b72f6f51SAlexander Aring u8 priv[0] __aligned(sizeof(void *)); 104b72f6f51SAlexander Aring }; 105b72f6f51SAlexander Aring 106b72f6f51SAlexander Aring static inline 107b72f6f51SAlexander Aring struct lowpan_priv *lowpan_priv(const struct net_device *dev) 108b72f6f51SAlexander Aring { 109b72f6f51SAlexander Aring return netdev_priv(dev); 110b72f6f51SAlexander Aring } 111b72f6f51SAlexander Aring 11272a5e6bbSAlexander Aring struct lowpan_802154_cb { 11372a5e6bbSAlexander Aring u16 d_tag; 11472a5e6bbSAlexander Aring unsigned int d_size; 11572a5e6bbSAlexander Aring u8 d_offset; 11672a5e6bbSAlexander Aring }; 11772a5e6bbSAlexander Aring 11872a5e6bbSAlexander Aring static inline 11972a5e6bbSAlexander Aring struct lowpan_802154_cb *lowpan_802154_cb(const struct sk_buff *skb) 12072a5e6bbSAlexander Aring { 12172a5e6bbSAlexander Aring BUILD_BUG_ON(sizeof(struct lowpan_802154_cb) > sizeof(skb->cb)); 12272a5e6bbSAlexander Aring return (struct lowpan_802154_cb *)skb->cb; 12372a5e6bbSAlexander Aring } 12472a5e6bbSAlexander Aring 125cefc8c8aSAlexander Aring #ifdef DEBUG 126cefc8c8aSAlexander Aring /* print data in line */ 127cefc8c8aSAlexander Aring static inline void raw_dump_inline(const char *caller, char *msg, 128a6f77389SAlexander Aring const unsigned char *buf, int len) 129cefc8c8aSAlexander Aring { 130cefc8c8aSAlexander Aring if (msg) 131cefc8c8aSAlexander Aring pr_debug("%s():%s: ", caller, msg); 132cefc8c8aSAlexander Aring 133cefc8c8aSAlexander Aring print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, buf, len, false); 134cefc8c8aSAlexander Aring } 135cefc8c8aSAlexander Aring 136cefc8c8aSAlexander Aring /* print data in a table format: 137cefc8c8aSAlexander Aring * 138cefc8c8aSAlexander Aring * addr: xx xx xx xx xx xx 139cefc8c8aSAlexander Aring * addr: xx xx xx xx xx xx 140cefc8c8aSAlexander Aring * ... 141cefc8c8aSAlexander Aring */ 142cefc8c8aSAlexander Aring static inline void raw_dump_table(const char *caller, char *msg, 143a6f77389SAlexander Aring const unsigned char *buf, int len) 144cefc8c8aSAlexander Aring { 145cefc8c8aSAlexander Aring if (msg) 146cefc8c8aSAlexander Aring pr_debug("%s():%s:\n", caller, msg); 147cefc8c8aSAlexander Aring 148cefc8c8aSAlexander Aring print_hex_dump_debug("\t", DUMP_PREFIX_OFFSET, 16, 1, buf, len, false); 149cefc8c8aSAlexander Aring } 150cefc8c8aSAlexander Aring #else 151cefc8c8aSAlexander Aring static inline void raw_dump_table(const char *caller, char *msg, 152a6f77389SAlexander Aring const unsigned char *buf, int len) { } 153cefc8c8aSAlexander Aring static inline void raw_dump_inline(const char *caller, char *msg, 154a6f77389SAlexander Aring const unsigned char *buf, int len) { } 155cefc8c8aSAlexander Aring #endif 156cefc8c8aSAlexander Aring 157478208e3SAlexander Aring /** 158478208e3SAlexander Aring * lowpan_fetch_skb - getting inline data from 6LoWPAN header 159478208e3SAlexander Aring * 160478208e3SAlexander Aring * This function will pull data from sk buffer and put it into data to 161478208e3SAlexander Aring * remove the 6LoWPAN inline data. This function returns true if the 162478208e3SAlexander Aring * sk buffer is too small to pull the amount of data which is specified 163478208e3SAlexander Aring * by len. 164478208e3SAlexander Aring * 165478208e3SAlexander Aring * @skb: the buffer where the inline data should be pulled from. 166478208e3SAlexander Aring * @data: destination buffer for the inline data. 167478208e3SAlexander Aring * @len: amount of data which should be pulled in bytes. 168478208e3SAlexander Aring */ 169478208e3SAlexander Aring static inline bool lowpan_fetch_skb(struct sk_buff *skb, void *data, 170478208e3SAlexander Aring unsigned int len) 171cefc8c8aSAlexander Aring { 172cefc8c8aSAlexander Aring if (unlikely(!pskb_may_pull(skb, len))) 173cefc8c8aSAlexander Aring return true; 174cefc8c8aSAlexander Aring 175cefc8c8aSAlexander Aring skb_copy_from_linear_data(skb, data, len); 176cefc8c8aSAlexander Aring skb_pull(skb, len); 177cefc8c8aSAlexander Aring 178cefc8c8aSAlexander Aring return false; 179cefc8c8aSAlexander Aring } 180cefc8c8aSAlexander Aring 181cefc8c8aSAlexander Aring static inline void lowpan_push_hc_data(u8 **hc_ptr, const void *data, 182cefc8c8aSAlexander Aring const size_t len) 183cefc8c8aSAlexander Aring { 184cefc8c8aSAlexander Aring memcpy(*hc_ptr, data, len); 185cefc8c8aSAlexander Aring *hc_ptr += len; 186cefc8c8aSAlexander Aring } 187cefc8c8aSAlexander Aring 18800f59314SAlexander Aring int lowpan_register_netdevice(struct net_device *dev, 18900f59314SAlexander Aring enum lowpan_lltypes lltype); 19000f59314SAlexander Aring int lowpan_register_netdev(struct net_device *dev, 19100f59314SAlexander Aring enum lowpan_lltypes lltype); 19200f59314SAlexander Aring void lowpan_unregister_netdevice(struct net_device *dev); 19300f59314SAlexander Aring void lowpan_unregister_netdev(struct net_device *dev); 194b72f6f51SAlexander Aring 1958911d774SAlexander Aring /** 1968911d774SAlexander Aring * lowpan_header_decompress - replace 6LoWPAN header with IPv6 header 1978911d774SAlexander Aring * 1988911d774SAlexander Aring * This function replaces the IPHC 6LoWPAN header which should be pointed at 1998911d774SAlexander Aring * skb->data and skb_network_header, with the IPv6 header. 2008911d774SAlexander Aring * It would be nice that the caller have the necessary headroom of IPv6 header 2018911d774SAlexander Aring * and greatest Transport layer header, this would reduce the overhead for 2028911d774SAlexander Aring * reallocate headroom. 2038911d774SAlexander Aring * 2048911d774SAlexander Aring * @skb: the buffer which should be manipulate. 2058911d774SAlexander Aring * @dev: the lowpan net device pointer. 2068911d774SAlexander Aring * @daddr: destination lladdr of mac header which is used for compression 2078911d774SAlexander Aring * methods. 2088911d774SAlexander Aring * @saddr: source lladdr of mac header which is used for compression 2098911d774SAlexander Aring * methods. 2108911d774SAlexander Aring */ 2118911d774SAlexander Aring int lowpan_header_decompress(struct sk_buff *skb, const struct net_device *dev, 2128911d774SAlexander Aring const void *daddr, const void *saddr); 213a6f77389SAlexander Aring 214a6f77389SAlexander Aring /** 215a6f77389SAlexander Aring * lowpan_header_compress - replace IPv6 header with 6LoWPAN header 216a6f77389SAlexander Aring * 217a6f77389SAlexander Aring * This function replaces the IPv6 header which should be pointed at 218a6f77389SAlexander Aring * skb->data and skb_network_header, with the IPHC 6LoWPAN header. 219a6f77389SAlexander Aring * The caller need to be sure that the sk buffer is not shared and at have 220a6f77389SAlexander Aring * at least a headroom which is smaller or equal LOWPAN_IPHC_MAX_HEADER_LEN, 221a6f77389SAlexander Aring * which is the IPHC "more bytes than IPv6 header" at worst case. 222a6f77389SAlexander Aring * 223a6f77389SAlexander Aring * @skb: the buffer which should be manipulate. 224a6f77389SAlexander Aring * @dev: the lowpan net device pointer. 225a6f77389SAlexander Aring * @daddr: destination lladdr of mac header which is used for compression 226a6f77389SAlexander Aring * methods. 227a6f77389SAlexander Aring * @saddr: source lladdr of mac header which is used for compression 228a6f77389SAlexander Aring * methods. 229a6f77389SAlexander Aring */ 230a6f77389SAlexander Aring int lowpan_header_compress(struct sk_buff *skb, const struct net_device *dev, 231a6f77389SAlexander Aring const void *daddr, const void *saddr); 232cefc8c8aSAlexander Aring 233cefc8c8aSAlexander Aring #endif /* __6LOWPAN_H__ */ 234