ReactOS 0.4.16-dev-1063-gd722e70
lowpan6_ble.c
Go to the documentation of this file.
1
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
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
87static ip6_addr_t rfc7668_context[LWIP_6LOWPAN_NUM_CONTEXTS];
88#else
89#define rfc7668_context NULL
90#endif
91
92static struct lowpan6_link_addr rfc7668_local_addr;
93static struct lowpan6_link_addr rfc7668_peer_addr;
94
109void
110ble_addr_to_eui64(u8_t *dst, const u8_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
138void
139eui64_to_ble_addr(u8_t *dst, const u8_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
149static err_t
150rfc7668_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
175err_t
176rfc7668_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... */
180 return rfc7668_set_addr(&rfc7668_local_addr, local_addr, local_addr_len, 0, 0);
181}
182
186err_t
187rfc7668_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... */
191 return rfc7668_set_addr(&rfc7668_local_addr, local_addr, local_addr_len, 1, is_public_addr);
192}
193
197err_t
198rfc7668_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... */
202 return rfc7668_set_addr(&rfc7668_peer_addr, peer_addr, peer_addr_len, 0, 0);
203}
204
208err_t
209rfc7668_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... */
213 return rfc7668_set_addr(&rfc7668_peer_addr, peer_addr, peer_addr_len, 1, is_public_addr);
214}
215
229static err_t
230rfc7668_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
299err_t
300rfc7668_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 */
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
313 return ERR_VAL;
314#endif
315}
316
327err_t
328rfc7668_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
346err_t
347rfc7668_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. */
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;
385 for (i = 0; i < p->len; i++) {
386 if ((i%4)==0) {
388 }
389 LWIP_DEBUGF(LWIP_RFC7668_IP_UNCOMPRESSED_DEBUG, ("%2X ", *((u8_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
409err_t
410rfc7668_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 */
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
439err_t
440tcpip_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 */
#define NULL
Definition: types.h:112
unsigned int idx
Definition: utils.c:41
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:158
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
#define MIB2_STATS_NETIF_ADD(n, x, val)
Definition: snmp.h:140
#define MIB2_STATS_NETIF_INC(n, x)
Definition: snmp.h:139
#define MIB2_INIT_NETIF(netif, type, speed)
Definition: snmp.h:138
#define ERR_MEM
Definition: fontsub.h:52
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLenum src
Definition: glext.h:6340
GLuint buffer
Definition: glext.h:5915
GLenum GLenum dst
Definition: glext.h:6340
GLenum const GLvoid * addr
Definition: glext.h:9621
GLfloat GLfloat p
Definition: glext.h:8902
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
uint8_t u8_t
Definition: arch.h:125
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:373
uint16_t u16_t
Definition: arch.h:127
#define LWIP_DBG_TRACE
Definition: debug.h:83
s8_t err_t
Definition: err.h:96
@ ERR_IF
Definition: err.h:79
@ ERR_OK
Definition: err.h:55
@ ERR_VAL
Definition: err.h:67
@ ERR_ARG
Definition: err.h:88
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:224
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:727
u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:1058
@ PBUF_RAM
Definition: pbuf.h:152
@ PBUF_RAW
Definition: pbuf.h:111
if(dx< 0)
Definition: linetemp.h:194
#define LWIP_6LOWPAN_NUM_CONTEXTS
Definition: lowpan6_opts.h:50
#define LWIP_RFC7668_IP_UNCOMPRESSED_DEBUG
Definition: lowpan6_opts.h:109
#define LWIP_LOWPAN6_DECOMPRESSION_DEBUG
Definition: lowpan6_opts.h:103
#define LWIP_LOWPAN6_DEBUG
Definition: lowpan6_opts.h:85
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
u8_t pbuf_remove_header(struct pbuf *p, size_t header_size_decrement)
Definition: pbuf.c:585
#define IP6_MIN_MTU_LENGTH
Definition: ip6.h:47
#define err(...)
Definition: http.c:7252
Definition: tcpip.h:126
Definition: netif.h:269
u8_t flags
Definition: netif.h:354
char name[2]
Definition: netif.h:356
u16_t mtu
Definition: netif.h:344
netif_linkoutput_fn linkoutput
Definition: netif.h:308
Definition: pbuf.h:186
u16_t tot_len
Definition: pbuf.h:200
u16_t len
Definition: pbuf.h:203
void * payload
Definition: pbuf.h:191
err_t tcpip_inpkt(struct pbuf *p, struct netif *inp, netif_input_fn input_fn)
Definition: tcpip.c:245