• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * 6LowPAN over BLE output for IPv6 (RFC7668).
4 */
5 
6 /*
7  * Copyright (c) 2017 Benjamin Aigner
8  * Copyright (c) 2015 Inico Technologies Ltd. , Author: Ivan Delamer <delamer@inicotech.com>
9  *
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without modification,
13  * are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  *    this list of conditions and the following disclaimer in the documentation
19  *    and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
26  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
28  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32  * OF SUCH DAMAGE.
33  *
34  * Author: Benjamin Aigner <aignerb@technikum-wien.at>
35  *
36  * Based on the original 6lowpan implementation of lwIP ( @see 6lowpan.c)
37  */
38 
39 
40 /**
41  * @defgroup rfc7668if 6LoWPAN over BLE (RFC7668)
42  * @ingroup netifs
43  * This file implements a RFC7668 implementation for 6LoWPAN over
44  * Bluetooth Low Energy. The specification is very similar to 6LoWPAN,
45  * so most of the code is re-used.
46  * Compared to 6LoWPAN, much functionality is already implemented in
47  * lower BLE layers (fragmenting, session management,...).
48  *
49  * Usage:
50  * - add this netif
51  *   - don't add IPv4 addresses (no IPv4 support in RFC7668), pass 'NULL','NULL','NULL'
52  *   - use the BLE to EUI64 conversation util to create an IPv6 link-local address from the BLE MAC (@ref ble_addr_to_eui64)
53  *   - input function: @ref rfc7668_input
54  * - set the link output function, which transmits output data to an established L2CAP channel
55  * - If data arrives (HCI event "L2CAP_DATA_PACKET"):
56  *   - allocate a @ref PBUF_RAW buffer
57  *   - let the pbuf struct point to the incoming data or copy it to the buffer
58  *   - call netif->input
59  *
60  * @todo:
61  * - further testing
62  * - support compression contexts
63  * - support multiple addresses
64  * - support multicast
65  * - support neighbor discovery
66  */
67 
68 
69 #include "netif/lowpan6_ble.h"
70 
71 #if LWIP_IPV6
72 
73 #include "lwip/ip.h"
74 #include "lwip/pbuf.h"
75 #include "lwip/ip_addr.h"
76 #include "lwip/netif.h"
77 #include "lwip/nd6.h"
78 #include "lwip/mem.h"
79 #include "lwip/udp.h"
80 #include "lwip/tcpip.h"
81 #include "lwip/snmp.h"
82 
83 #include <string.h>
84 
85 #if LWIP_6LOWPAN_NUM_CONTEXTS > 0
86 /** context memory, containing IPv6 addresses */
87 static ip6_addr_t rfc7668_context[LWIP_6LOWPAN_NUM_CONTEXTS];
88 #else
89 #define rfc7668_context NULL
90 #endif
91 
92 static struct lowpan6_link_addr rfc7668_local_addr;
93 static struct lowpan6_link_addr rfc7668_peer_addr;
94 
95 /**
96  * @ingroup rfc7668if
97  *  convert BT address to EUI64 addr
98  *
99  * This method converts a Bluetooth MAC address to an EUI64 address,
100  * which is used within IPv6 communication
101  *
102  * @param dst IPv6 destination space
103  * @param src BLE MAC address source
104  * @param public_addr If the LWIP_RFC7668_LINUX_WORKAROUND_PUBLIC_ADDRESS
105  * option is set, bit 0x02 will be set if param=0 (no public addr); cleared otherwise
106  *
107  * @see LWIP_RFC7668_LINUX_WORKAROUND_PUBLIC_ADDRESS
108  */
109 void
ble_addr_to_eui64(uint8_t * dst,const uint8_t * src,int public_addr)110 ble_addr_to_eui64(uint8_t *dst, const uint8_t *src, int public_addr)
111 {
112   /* according to RFC7668 ch 3.2.2. */
113   memcpy(dst, src, 3);
114   dst[3] = 0xFF;
115   dst[4] = 0xFE;
116   memcpy(&dst[5], &src[3], 3);
117 #if LWIP_RFC7668_LINUX_WORKAROUND_PUBLIC_ADDRESS
118   if(public_addr) {
119     dst[0] &= ~0x02;
120   } else {
121     dst[0] |= 0x02;
122   }
123 #else
124   LWIP_UNUSED_ARG(public_addr);
125 #endif
126 }
127 
128 /**
129  * @ingroup rfc7668if
130  *  convert EUI64 address to Bluetooth MAC addr
131  *
132  * This method converts an EUI64 address to a Bluetooth MAC address,
133  *
134  * @param dst BLE MAC address destination
135  * @param src IPv6 source
136  *
137  */
138 void
eui64_to_ble_addr(uint8_t * dst,const uint8_t * src)139 eui64_to_ble_addr(uint8_t *dst, const uint8_t *src)
140 {
141   /* according to RFC7668 ch 3.2.2. */
142   memcpy(dst,src,3);
143   memcpy(&dst[3],&src[5],3);
144 }
145 
146 /** Set an address used for stateful compression.
147  * This expects an address of 6 or 8 bytes.
148  */
149 static err_t
rfc7668_set_addr(struct lowpan6_link_addr * addr,const u8_t * in_addr,size_t in_addr_len,int is_mac_48,int is_public_addr)150 rfc7668_set_addr(struct lowpan6_link_addr *addr, const u8_t *in_addr, size_t in_addr_len, int is_mac_48, int is_public_addr)
151 {
152   if ((in_addr == NULL) || (addr == NULL)) {
153     return ERR_VAL;
154   }
155   if (is_mac_48) {
156     if (in_addr_len != 6) {
157       return ERR_VAL;
158     }
159     addr->addr_len = 8;
160     ble_addr_to_eui64(addr->addr, in_addr, is_public_addr);
161   } else {
162     if (in_addr_len != 8) {
163       return ERR_VAL;
164     }
165     addr->addr_len = 8;
166     memcpy(addr->addr, in_addr, 8);
167   }
168   return ERR_OK;
169 }
170 
171 
172 /** Set the local address used for stateful compression.
173  * This expects an address of 8 bytes.
174  */
175 err_t
rfc7668_set_local_addr_eui64(struct netif * netif,const u8_t * local_addr,size_t local_addr_len)176 rfc7668_set_local_addr_eui64(struct netif *netif, const u8_t *local_addr, size_t local_addr_len)
177 {
178   /* netif not used for now, the address is stored globally... */
179   LWIP_UNUSED_ARG(netif);
180   return rfc7668_set_addr(&rfc7668_local_addr, local_addr, local_addr_len, 0, 0);
181 }
182 
183 /** Set the local address used for stateful compression.
184  * This expects an address of 6 bytes.
185  */
186 err_t
rfc7668_set_local_addr_mac48(struct netif * netif,const u8_t * local_addr,size_t local_addr_len,int is_public_addr)187 rfc7668_set_local_addr_mac48(struct netif *netif, const u8_t *local_addr, size_t local_addr_len, int is_public_addr)
188 {
189   /* netif not used for now, the address is stored globally... */
190   LWIP_UNUSED_ARG(netif);
191   return rfc7668_set_addr(&rfc7668_local_addr, local_addr, local_addr_len, 1, is_public_addr);
192 }
193 
194 /** Set the peer address used for stateful compression.
195  * This expects an address of 8 bytes.
196  */
197 err_t
rfc7668_set_peer_addr_eui64(struct netif * netif,const u8_t * peer_addr,size_t peer_addr_len)198 rfc7668_set_peer_addr_eui64(struct netif *netif, const u8_t *peer_addr, size_t peer_addr_len)
199 {
200   /* netif not used for now, the address is stored globally... */
201   LWIP_UNUSED_ARG(netif);
202   return rfc7668_set_addr(&rfc7668_peer_addr, peer_addr, peer_addr_len, 0, 0);
203 }
204 
205 /** Set the peer address used for stateful compression.
206  * This expects an address of 6 bytes.
207  */
208 err_t
rfc7668_set_peer_addr_mac48(struct netif * netif,const u8_t * peer_addr,size_t peer_addr_len,int is_public_addr)209 rfc7668_set_peer_addr_mac48(struct netif *netif, const u8_t *peer_addr, size_t peer_addr_len, int is_public_addr)
210 {
211   /* netif not used for now, the address is stored globally... */
212   LWIP_UNUSED_ARG(netif);
213   return rfc7668_set_addr(&rfc7668_peer_addr, peer_addr, peer_addr_len, 1, is_public_addr);
214 }
215 
216 /** Encapsulate IPv6 frames for BLE transmission
217  *
218  * This method implements the IPv6 header compression:
219  *  *) According to RFC6282
220  *  *) See Figure 2, contains base format of bit positions
221  *  *) Fragmentation not necessary (done at L2CAP layer of BLE)
222  * @note Currently the pbuf allocation uses 256 bytes. If longer packets are used (possible due to MTU=1480Bytes), increase it here!
223  *
224  * @param p Pbuf struct, containing the payload data
225  * @param netif Output network interface. Should be of RFC7668 type
226  *
227  * @return Same as netif->output.
228  */
229 static err_t
rfc7668_compress(struct netif * netif,struct pbuf * p)230 rfc7668_compress(struct netif *netif, struct pbuf *p)
231 {
232   struct pbuf *p_frag;
233   u16_t remaining_len;
234   u8_t *buffer;
235   u8_t lowpan6_header_len;
236   u8_t hidden_header_len;
237   err_t err;
238 
239   LWIP_ASSERT("lowpan6_frag: netif->linkoutput not set", netif->linkoutput != NULL);
240 
241 #if LWIP_6LOWPAN_IPHC
242 
243   /* We'll use a dedicated pbuf for building BLE fragments.
244    * We'll over-allocate it by the bytes saved for header compression.
245    */
246   p_frag = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
247   if (p_frag == NULL) {
248     MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
249     return ERR_MEM;
250   }
251   LWIP_ASSERT("this needs a pbuf in one piece", p_frag->len == p_frag->tot_len);
252 
253   /* Write IP6 header (with IPHC). */
254   buffer = (u8_t*)p_frag->payload;
255 
256   err = lowpan6_compress_headers(netif, (u8_t *)p->payload, p->len, buffer, p_frag->len,
257     &lowpan6_header_len, &hidden_header_len, rfc7668_context, &rfc7668_local_addr, &rfc7668_peer_addr);
258   if (err != ERR_OK) {
259     MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
260     pbuf_free(p_frag);
261     return err;
262   }
263   pbuf_remove_header(p, hidden_header_len);
264 
265   /* Calculate remaining packet length */
266   remaining_len = p->tot_len;
267 
268   /* Copy IPv6 packet */
269   pbuf_copy_partial(p, buffer + lowpan6_header_len, remaining_len, 0);
270 
271   /* Calculate frame length */
272   p_frag->len = p_frag->tot_len = remaining_len + lowpan6_header_len;
273 
274   /* send the packet */
275   MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p_frag->tot_len);
276   LWIP_DEBUGF(LWIP_LOWPAN6_DEBUG|LWIP_DBG_TRACE, ("rfc7668_output: sending packet %p\n", (void *)p));
277   err = netif->linkoutput(netif, p_frag);
278 
279   pbuf_free(p_frag);
280 
281   return err;
282 #else /* LWIP_6LOWPAN_IPHC */
283   /* 6LoWPAN over BLE requires IPHC! */
284   return ERR_IF;
285 #endif/* LWIP_6LOWPAN_IPHC */
286 }
287 
288 /**
289  * @ingroup rfc7668if
290  * Set context id IPv6 address
291  *
292  * Store one IPv6 address to a given context id.
293  *
294  * @param idx Context id
295  * @param context IPv6 addr for this context
296  *
297  * @return ERR_OK (if everything is fine), ERR_ARG (if the context id is out of range), ERR_VAL (if contexts disabled)
298  */
299 err_t
rfc7668_set_context(u8_t idx,const ip6_addr_t * context)300 rfc7668_set_context(u8_t idx, const ip6_addr_t *context)
301 {
302 #if LWIP_6LOWPAN_NUM_CONTEXTS > 0
303   /* check if the ID is possible */
304   if (idx >= LWIP_6LOWPAN_NUM_CONTEXTS) {
305     return ERR_ARG;
306   }
307   /* copy IPv6 address to context storage */
308   ip6_addr_set(&rfc7668_context[idx], context);
309   return ERR_OK;
310 #else
311   LWIP_UNUSED_ARG(idx);
312   LWIP_UNUSED_ARG(context);
313   return ERR_VAL;
314 #endif
315 }
316 
317 /**
318  * @ingroup rfc7668if
319  * Compress outgoing IPv6 packet and pass it on to netif->linkoutput
320  *
321  * @param netif The lwIP network interface which the IP packet will be sent on.
322  * @param q The pbuf(s) containing the IP packet to be sent.
323  * @param ip6addr The IP address of the packet destination.
324  *
325  * @return See rfc7668_compress
326  */
327 err_t
rfc7668_output(struct netif * netif,struct pbuf * q,const ip6_addr_t * ip6addr)328 rfc7668_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr)
329 {
330   /* dst ip6addr is not used here, we only have one peer */
331   LWIP_UNUSED_ARG(ip6addr);
332 
333   return rfc7668_compress(netif, q);
334 }
335 
336 /**
337  * @ingroup rfc7668if
338  * Process a received raw payload from an L2CAP channel
339  *
340  * @param p the received packet, p->payload pointing to the
341  *        IPv6 header (maybe compressed)
342  * @param netif the network interface on which the packet was received
343  *
344  * @return ERR_OK if everything was fine
345  */
346 err_t
rfc7668_input(struct pbuf * p,struct netif * netif)347 rfc7668_input(struct pbuf * p, struct netif *netif)
348 {
349   u8_t * puc;
350 
351   MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
352 
353   /* Load first header byte */
354   puc = (u8_t*)p->payload;
355 
356   /* no IP header compression */
357   if (*puc == 0x41) {
358     LWIP_DEBUGF(LWIP_LOWPAN6_DECOMPRESSION_DEBUG, ("Completed packet, removing dispatch: 0x%2x \n", *puc));
359     /* This is a complete IPv6 packet, just skip header byte. */
360     pbuf_remove_header(p, 1);
361   /* IPHC header compression */
362   } else if ((*puc & 0xe0 )== 0x60) {
363     LWIP_DEBUGF(LWIP_LOWPAN6_DECOMPRESSION_DEBUG, ("Completed packet, decompress dispatch: 0x%2x \n", *puc));
364     /* IPv6 headers are compressed using IPHC. */
365     p = lowpan6_decompress(p, 0, rfc7668_context, &rfc7668_peer_addr, &rfc7668_local_addr);
366     /* if no pbuf is returned, handle as discarded packet */
367     if (p == NULL) {
368       MIB2_STATS_NETIF_INC(netif, ifindiscards);
369       return ERR_OK;
370     }
371   /* invalid header byte, discard */
372   } else {
373     LWIP_DEBUGF(LWIP_LOWPAN6_DECOMPRESSION_DEBUG, ("Completed packet, discarding: 0x%2x \n", *puc));
374     MIB2_STATS_NETIF_INC(netif, ifindiscards);
375     pbuf_free(p);
376     return ERR_OK;
377   }
378   /* @todo: distinguish unicast/multicast */
379   MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
380 
381 #if LWIP_RFC7668_IP_UNCOMPRESSED_DEBUG
382   {
383     u16_t i;
384     LWIP_DEBUGF(LWIP_RFC7668_IP_UNCOMPRESSED_DEBUG, ("IPv6 payload:\n"));
385     for (i = 0; i < p->len; i++) {
386       if ((i%4)==0) {
387         LWIP_DEBUGF(LWIP_RFC7668_IP_UNCOMPRESSED_DEBUG, ("\n"));
388       }
389       LWIP_DEBUGF(LWIP_RFC7668_IP_UNCOMPRESSED_DEBUG, ("%2X ", *((uint8_t *)p->payload+i)));
390     }
391     LWIP_DEBUGF(LWIP_RFC7668_IP_UNCOMPRESSED_DEBUG, ("\np->len: %d\n", p->len));
392   }
393 #endif
394   /* pass data to ip6_input */
395   return ip6_input(p, netif);
396 }
397 
398 /**
399  * @ingroup rfc7668if
400  * Initialize the netif
401  *
402  * No flags are used (broadcast not possible, not ethernet, ...)
403  * The shortname for this netif is "BT"
404  *
405  * @param netif the network interface to be initialized as RFC7668 netif
406  *
407  * @return ERR_OK if everything went fine
408  */
409 err_t
rfc7668_if_init(struct netif * netif)410 rfc7668_if_init(struct netif *netif)
411 {
412   netif->name[0] = 'b';
413   netif->name[1] = 't';
414   /* local function as IPv6 output */
415   netif->output_ip6 = rfc7668_output;
416 
417   MIB2_INIT_NETIF(netif, snmp_ifType_other, 0);
418 
419   /* maximum transfer unit, set according to RFC7668 ch2.4 */
420   netif->mtu = IP6_MIN_MTU_LENGTH;
421 
422   /* no flags set (no broadcast, ethernet,...)*/
423   netif->flags = 0;
424 
425   /* everything fine */
426   return ERR_OK;
427 }
428 
429 #if !NO_SYS
430 /**
431  * Pass a received packet to tcpip_thread for input processing
432  *
433  * @param p the received packet, p->payload pointing to the
434  *          IEEE 802.15.4 header.
435  * @param inp the network interface on which the packet was received
436  *
437  * @return see @ref tcpip_inpkt, same return values
438  */
439 err_t
tcpip_rfc7668_input(struct pbuf * p,struct netif * inp)440 tcpip_rfc7668_input(struct pbuf *p, struct netif *inp)
441 {
442   /* send data to upper layer, return the result */
443   return tcpip_inpkt(p, inp, rfc7668_input);
444 }
445 #endif /* !NO_SYS */
446 
447 #endif /* LWIP_IPV6 */
448