source: S-port/trunk/Middlewares/Third_Party/LwIP/src/netif/lowpan6.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 29.4 KB
Line 
1/**
2 * @file
3 *
4 * 6LowPAN output for IPv6. Uses ND tables for link-layer addressing. Fragments packets to 6LowPAN units.
5 *
6 * This implementation aims to conform to IEEE 802.15.4(-2015), RFC 4944 and RFC 6282.
7 * @todo: RFC 6775.
8 */
9
10/*
11 * Copyright (c) 2015 Inico Technologies Ltd.
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without modification,
15 * are permitted provided that the following conditions are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright notice,
20 * this list of conditions and the following disclaimer in the documentation
21 * and/or other materials provided with the distribution.
22 * 3. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
28 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
30 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 * OF SUCH DAMAGE.
35 *
36 * This file is part of the lwIP TCP/IP stack.
37 *
38 * Author: Ivan Delamer <delamer@inicotech.com>
39 *
40 *
41 * Please coordinate changes and requests with Ivan Delamer
42 * <delamer@inicotech.com>
43 */
44
45/**
46 * @defgroup sixlowpan 6LoWPAN (RFC4944)
47 * @ingroup netifs
48 * 6LowPAN netif implementation
49 */
50
51#include "netif/lowpan6.h"
52
53#if LWIP_IPV6
54
55#include "lwip/ip.h"
56#include "lwip/pbuf.h"
57#include "lwip/ip_addr.h"
58#include "lwip/netif.h"
59#include "lwip/nd6.h"
60#include "lwip/mem.h"
61#include "lwip/udp.h"
62#include "lwip/tcpip.h"
63#include "lwip/snmp.h"
64#include "netif/ieee802154.h"
65
66#include <string.h>
67
68#if LWIP_6LOWPAN_802154_HW_CRC
69#define LWIP_6LOWPAN_DO_CALC_CRC(buf, len) 0
70#else
71#define LWIP_6LOWPAN_DO_CALC_CRC(buf, len) LWIP_6LOWPAN_CALC_CRC(buf, len)
72#endif
73
74/** This is a helper struct for reassembly of fragments
75 * (IEEE 802.15.4 limits to 127 bytes)
76 */
77struct lowpan6_reass_helper {
78 struct lowpan6_reass_helper *next_packet;
79 struct pbuf *reass;
80 struct pbuf *frags;
81 u8_t timer;
82 struct lowpan6_link_addr sender_addr;
83 u16_t datagram_size;
84 u16_t datagram_tag;
85};
86
87/** This struct keeps track of per-netif state */
88struct lowpan6_ieee802154_data {
89 /** fragment reassembly list */
90 struct lowpan6_reass_helper *reass_list;
91#if LWIP_6LOWPAN_NUM_CONTEXTS > 0
92 /** address context for compression */
93 ip6_addr_t lowpan6_context[LWIP_6LOWPAN_NUM_CONTEXTS];
94#endif
95 /** Datagram Tag for fragmentation */
96 u16_t tx_datagram_tag;
97 /** local PAN ID for IEEE 802.15.4 header */
98 u16_t ieee_802154_pan_id;
99 /** Sequence Number for IEEE 802.15.4 transmission */
100 u8_t tx_frame_seq_num;
101};
102
103/* Maximum frame size is 127 bytes minus CRC size */
104#define LOWPAN6_MAX_PAYLOAD (127 - 2)
105
106/** Currently, this state is global, since there's only one 6LoWPAN netif */
107static struct lowpan6_ieee802154_data lowpan6_data;
108
109#if LWIP_6LOWPAN_NUM_CONTEXTS > 0
110#define LWIP_6LOWPAN_CONTEXTS(netif) lowpan6_data.lowpan6_context
111#else
112#define LWIP_6LOWPAN_CONTEXTS(netif) NULL
113#endif
114
115static const struct lowpan6_link_addr ieee_802154_broadcast = {2, {0xff, 0xff}};
116
117#if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
118static struct lowpan6_link_addr short_mac_addr = {2, {0, 0}};
119#endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
120
121/* IEEE 802.15.4 specific functions: */
122
123/** Write the IEEE 802.15.4 header that encapsulates the 6LoWPAN frame.
124 * Src and dst PAN IDs are filled with the ID set by @ref lowpan6_set_pan_id.
125 *
126 * Since the length is variable:
127 * @returns the header length
128 */
129static u8_t
130lowpan6_write_iee802154_header(struct ieee_802154_hdr *hdr, const struct lowpan6_link_addr *src,
131 const struct lowpan6_link_addr *dst)
132{
133 u8_t ieee_header_len;
134 u8_t *buffer;
135 u8_t i;
136 u16_t fc;
137
138 fc = IEEE_802154_FC_FT_DATA; /* send data packet (2003 frame version) */
139 fc |= IEEE_802154_FC_PANID_COMPR; /* set PAN ID compression, for now src and dst PANs are equal */
140 if (dst != &ieee_802154_broadcast) {
141 fc |= IEEE_802154_FC_ACK_REQ; /* data packet, no broadcast: ack required. */
142 }
143 if (dst->addr_len == 2) {
144 fc |= IEEE_802154_FC_DST_ADDR_MODE_SHORT;
145 } else {
146 LWIP_ASSERT("invalid dst address length", dst->addr_len == 8);
147 fc |= IEEE_802154_FC_DST_ADDR_MODE_EXT;
148 }
149 if (src->addr_len == 2) {
150 fc |= IEEE_802154_FC_SRC_ADDR_MODE_SHORT;
151 } else {
152 LWIP_ASSERT("invalid src address length", src->addr_len == 8);
153 fc |= IEEE_802154_FC_SRC_ADDR_MODE_EXT;
154 }
155 hdr->frame_control = fc;
156 hdr->sequence_number = lowpan6_data.tx_frame_seq_num++;
157 hdr->destination_pan_id = lowpan6_data.ieee_802154_pan_id; /* pan id */
158
159 buffer = (u8_t *)hdr;
160 ieee_header_len = 5;
161 i = dst->addr_len;
162 /* reverse memcpy of dst addr */
163 while (i-- > 0) {
164 buffer[ieee_header_len++] = dst->addr[i];
165 }
166 /* Source PAN ID skipped due to PAN ID Compression */
167 i = src->addr_len;
168 /* reverse memcpy of src addr */
169 while (i-- > 0) {
170 buffer[ieee_header_len++] = src->addr[i];
171 }
172 return ieee_header_len;
173}
174
175/** Parse the IEEE 802.15.4 header from a pbuf.
176 * If successful, the header is hidden from the pbuf.
177 *
178 * PAN IDs and seuqence number are not checked
179 *
180 * @param p input pbuf, p->payload pointing at the IEEE 802.15.4 header
181 * @param src pointer to source address filled from the header
182 * @param dest pointer to destination address filled from the header
183 * @returns ERR_OK if successful
184 */
185static err_t
186lowpan6_parse_iee802154_header(struct pbuf *p, struct lowpan6_link_addr *src,
187 struct lowpan6_link_addr *dest)
188{
189 u8_t *puc;
190 s8_t i;
191 u16_t frame_control, addr_mode;
192 u16_t datagram_offset;
193
194 /* Parse IEEE 802.15.4 header */
195 puc = (u8_t *)p->payload;
196 frame_control = puc[0] | (puc[1] << 8);
197 datagram_offset = 2;
198 if (frame_control & IEEE_802154_FC_SEQNO_SUPPR) {
199 if (IEEE_802154_FC_FRAME_VERSION_GET(frame_control) <= 1) {
200 /* sequence number suppressed, this is not valid for versions 0/1 */
201 return ERR_VAL;
202 }
203 } else {
204 datagram_offset++;
205 }
206 datagram_offset += 2; /* Skip destination PAN ID */
207 addr_mode = frame_control & IEEE_802154_FC_DST_ADDR_MODE_MASK;
208 if (addr_mode == IEEE_802154_FC_DST_ADDR_MODE_EXT) {
209 /* extended address (64 bit) */
210 dest->addr_len = 8;
211 /* reverse memcpy: */
212 for (i = 0; i < 8; i++) {
213 dest->addr[i] = puc[datagram_offset + 7 - i];
214 }
215 datagram_offset += 8;
216 } else if (addr_mode == IEEE_802154_FC_DST_ADDR_MODE_SHORT) {
217 /* short address (16 bit) */
218 dest->addr_len = 2;
219 /* reverse memcpy: */
220 dest->addr[0] = puc[datagram_offset + 1];
221 dest->addr[1] = puc[datagram_offset];
222 datagram_offset += 2;
223 } else {
224 /* unsupported address mode (do we need "no address"?) */
225 return ERR_VAL;
226 }
227
228 if (!(frame_control & IEEE_802154_FC_PANID_COMPR)) {
229 /* No PAN ID compression, skip source PAN ID */
230 datagram_offset += 2;
231 }
232
233 addr_mode = frame_control & IEEE_802154_FC_SRC_ADDR_MODE_MASK;
234 if (addr_mode == IEEE_802154_FC_SRC_ADDR_MODE_EXT) {
235 /* extended address (64 bit) */
236 src->addr_len = 8;
237 /* reverse memcpy: */
238 for (i = 0; i < 8; i++) {
239 src->addr[i] = puc[datagram_offset + 7 - i];
240 }
241 datagram_offset += 8;
242 } else if (addr_mode == IEEE_802154_FC_DST_ADDR_MODE_SHORT) {
243 /* short address (16 bit) */
244 src->addr_len = 2;
245 src->addr[0] = puc[datagram_offset + 1];
246 src->addr[1] = puc[datagram_offset];
247 datagram_offset += 2;
248 } else {
249 /* unsupported address mode (do we need "no address"?) */
250 return ERR_VAL;
251 }
252
253 /* hide IEEE802.15.4 header. */
254 if (pbuf_remove_header(p, datagram_offset)) {
255 return ERR_VAL;
256 }
257 return ERR_OK;
258}
259
260/** Calculate the 16-bit CRC as required by IEEE 802.15.4 */
261u16_t
262lowpan6_calc_crc(const void* buf, u16_t len)
263{
264#define CCITT_POLY_16 0x8408U
265 u16_t i;
266 u8_t b;
267 u16_t crc = 0;
268 const u8_t* p = (const u8_t*)buf;
269
270 for (i = 0; i < len; i++) {
271 u8_t data = *p;
272 for (b = 0U; b < 8U; b++) {
273 if (((data ^ crc) & 1) != 0) {
274 crc = (u16_t)((crc >> 1) ^ CCITT_POLY_16);
275 } else {
276 crc = (u16_t)(crc >> 1);
277 }
278 data = (u8_t)(data >> 1);
279 }
280 p++;
281 }
282 return crc;
283}
284
285/* Fragmentation specific functions: */
286
287static void
288free_reass_datagram(struct lowpan6_reass_helper *lrh)
289{
290 if (lrh->reass) {
291 pbuf_free(lrh->reass);
292 }
293 if (lrh->frags) {
294 pbuf_free(lrh->frags);
295 }
296 mem_free(lrh);
297}
298
299/**
300 * Removes a datagram from the reassembly queue.
301 **/
302static void
303dequeue_datagram(struct lowpan6_reass_helper *lrh, struct lowpan6_reass_helper *prev)
304{
305 if (lowpan6_data.reass_list == lrh) {
306 lowpan6_data.reass_list = lowpan6_data.reass_list->next_packet;
307 } else {
308 /* it wasn't the first, so it must have a valid 'prev' */
309 LWIP_ASSERT("sanity check linked list", prev != NULL);
310 prev->next_packet = lrh->next_packet;
311 }
312}
313
314/**
315 * Periodic timer for 6LowPAN functions:
316 *
317 * - Remove incomplete/old packets
318 */
319void
320lowpan6_tmr(void)
321{
322 struct lowpan6_reass_helper *lrh, *lrh_next, *lrh_prev = NULL;
323
324 lrh = lowpan6_data.reass_list;
325 while (lrh != NULL) {
326 lrh_next = lrh->next_packet;
327 if ((--lrh->timer) == 0) {
328 dequeue_datagram(lrh, lrh_prev);
329 free_reass_datagram(lrh);
330 } else {
331 lrh_prev = lrh;
332 }
333 lrh = lrh_next;
334 }
335}
336
337/*
338 * Encapsulates data into IEEE 802.15.4 frames.
339 * Fragments an IPv6 datagram into 6LowPAN units, which fit into IEEE 802.15.4 frames.
340 * If configured, will compress IPv6 and or UDP headers.
341 * */
342static err_t
343lowpan6_frag(struct netif *netif, struct pbuf *p, const struct lowpan6_link_addr *src, const struct lowpan6_link_addr *dst)
344{
345 struct pbuf *p_frag;
346 u16_t frag_len, remaining_len, max_data_len;
347 u8_t *buffer;
348 u8_t ieee_header_len;
349 u8_t lowpan6_header_len;
350 u8_t hidden_header_len;
351 u16_t crc;
352 u16_t datagram_offset;
353 err_t err = ERR_IF;
354
355 LWIP_ASSERT("lowpan6_frag: netif->linkoutput not set", netif->linkoutput != NULL);
356
357 /* We'll use a dedicated pbuf for building 6LowPAN fragments. */
358 p_frag = pbuf_alloc(PBUF_RAW, 127, PBUF_RAM);
359 if (p_frag == NULL) {
360 MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
361 return ERR_MEM;
362 }
363 LWIP_ASSERT("this needs a pbuf in one piece", p_frag->len == p_frag->tot_len);
364
365 /* Write IEEE 802.15.4 header. */
366 buffer = (u8_t *)p_frag->payload;
367 ieee_header_len = lowpan6_write_iee802154_header((struct ieee_802154_hdr *)buffer, src, dst);
368 LWIP_ASSERT("ieee_header_len < p_frag->len", ieee_header_len < p_frag->len);
369
370#if LWIP_6LOWPAN_IPHC
371 /* Perform 6LowPAN IPv6 header compression according to RFC 6282 */
372 /* do the header compression (this does NOT copy any non-compressed data) */
373 err = lowpan6_compress_headers(netif, (u8_t *)p->payload, p->len,
374 &buffer[ieee_header_len], p_frag->len - ieee_header_len, &lowpan6_header_len,
375 &hidden_header_len, LWIP_6LOWPAN_CONTEXTS(netif), src, dst);
376 if (err != ERR_OK) {
377 MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
378 pbuf_free(p_frag);
379 return err;
380 }
381 pbuf_remove_header(p, hidden_header_len);
382
383#else /* LWIP_6LOWPAN_IPHC */
384 /* Send uncompressed IPv6 header with appropriate dispatch byte. */
385 lowpan6_header_len = 1;
386 buffer[ieee_header_len] = 0x41; /* IPv6 dispatch */
387#endif /* LWIP_6LOWPAN_IPHC */
388
389 /* Calculate remaining packet length */
390 remaining_len = p->tot_len;
391
392 if (remaining_len > 0x7FF) {
393 MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
394 /* datagram_size must fit into 11 bit */
395 pbuf_free(p_frag);
396 return ERR_VAL;
397 }
398
399 /* Fragment, or 1 packet? */
400 max_data_len = LOWPAN6_MAX_PAYLOAD - ieee_header_len - lowpan6_header_len;
401 if (remaining_len > max_data_len) {
402 u16_t data_len;
403 /* We must move the 6LowPAN header to make room for the FRAG header. */
404 memmove(&buffer[ieee_header_len + 4], &buffer[ieee_header_len], lowpan6_header_len);
405
406 /* Now we need to fragment the packet. FRAG1 header first */
407 buffer[ieee_header_len] = 0xc0 | (((p->tot_len + hidden_header_len) >> 8) & 0x7);
408 buffer[ieee_header_len + 1] = (p->tot_len + hidden_header_len) & 0xff;
409
410 lowpan6_data.tx_datagram_tag++;
411 buffer[ieee_header_len + 2] = (lowpan6_data.tx_datagram_tag >> 8) & 0xff;
412 buffer[ieee_header_len + 3] = lowpan6_data.tx_datagram_tag & 0xff;
413
414 /* Fragment follows. */
415 data_len = (max_data_len - 4) & 0xf8;
416 frag_len = data_len + lowpan6_header_len;
417
418 pbuf_copy_partial(p, buffer + ieee_header_len + lowpan6_header_len + 4, frag_len - lowpan6_header_len, 0);
419 remaining_len -= frag_len - lowpan6_header_len;
420 /* datagram offset holds the offset before compression */
421 datagram_offset = frag_len - lowpan6_header_len + hidden_header_len;
422 LWIP_ASSERT("datagram offset must be a multiple of 8", (datagram_offset & 7) == 0);
423
424 /* Calculate frame length */
425 p_frag->len = p_frag->tot_len = ieee_header_len + 4 + frag_len + 2; /* add 2 bytes for crc*/
426
427 /* 2 bytes CRC */
428 crc = LWIP_6LOWPAN_DO_CALC_CRC(p_frag->payload, p_frag->len - 2);
429 pbuf_take_at(p_frag, &crc, 2, p_frag->len - 2);
430
431 /* send the packet */
432 MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p_frag->tot_len);
433 LWIP_DEBUGF(LWIP_LOWPAN6_DEBUG | LWIP_DBG_TRACE, ("lowpan6_send: sending packet %p\n", (void *)p));
434 err = netif->linkoutput(netif, p_frag);
435
436 while ((remaining_len > 0) && (err == ERR_OK)) {
437 struct ieee_802154_hdr *hdr = (struct ieee_802154_hdr *)buffer;
438 /* new frame, new seq num for ACK */
439 hdr->sequence_number = lowpan6_data.tx_frame_seq_num++;
440
441 buffer[ieee_header_len] |= 0x20; /* Change FRAG1 to FRAGN */
442
443 LWIP_ASSERT("datagram offset must be a multiple of 8", (datagram_offset & 7) == 0);
444 buffer[ieee_header_len + 4] = (u8_t)(datagram_offset >> 3); /* datagram offset in FRAGN header (datagram_offset is max. 11 bit) */
445
446 frag_len = (127 - ieee_header_len - 5 - 2) & 0xf8;
447 if (frag_len > remaining_len) {
448 frag_len = remaining_len;
449 }
450
451 pbuf_copy_partial(p, buffer + ieee_header_len + 5, frag_len, p->tot_len - remaining_len);
452 remaining_len -= frag_len;
453 datagram_offset += frag_len;
454
455 /* Calculate frame length */
456 p_frag->len = p_frag->tot_len = frag_len + 5 + ieee_header_len + 2;
457
458 /* 2 bytes CRC */
459 crc = LWIP_6LOWPAN_DO_CALC_CRC(p_frag->payload, p_frag->len - 2);
460 pbuf_take_at(p_frag, &crc, 2, p_frag->len - 2);
461
462 /* send the packet */
463 MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p_frag->tot_len);
464 LWIP_DEBUGF(LWIP_LOWPAN6_DEBUG | LWIP_DBG_TRACE, ("lowpan6_send: sending packet %p\n", (void *)p));
465 err = netif->linkoutput(netif, p_frag);
466 }
467 } else {
468 /* It fits in one frame. */
469 frag_len = remaining_len;
470
471 /* Copy IPv6 packet */
472 pbuf_copy_partial(p, buffer + ieee_header_len + lowpan6_header_len, frag_len, 0);
473 remaining_len = 0;
474
475 /* Calculate frame length */
476 p_frag->len = p_frag->tot_len = frag_len + lowpan6_header_len + ieee_header_len + 2;
477 LWIP_ASSERT("", p_frag->len <= 127);
478
479 /* 2 bytes CRC */
480 crc = LWIP_6LOWPAN_DO_CALC_CRC(p_frag->payload, p_frag->len - 2);
481 pbuf_take_at(p_frag, &crc, 2, p_frag->len - 2);
482
483 /* send the packet */
484 MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p_frag->tot_len);
485 LWIP_DEBUGF(LWIP_LOWPAN6_DEBUG | LWIP_DBG_TRACE, ("lowpan6_send: sending packet %p\n", (void *)p));
486 err = netif->linkoutput(netif, p_frag);
487 }
488
489 pbuf_free(p_frag);
490
491 return err;
492}
493
494/**
495 * @ingroup sixlowpan
496 * Set context
497 */
498err_t
499lowpan6_set_context(u8_t idx, const ip6_addr_t *context)
500{
501#if LWIP_6LOWPAN_NUM_CONTEXTS > 0
502 if (idx >= LWIP_6LOWPAN_NUM_CONTEXTS) {
503 return ERR_ARG;
504 }
505
506 IP6_ADDR_ZONECHECK(context);
507
508 ip6_addr_set(&lowpan6_data.lowpan6_context[idx], context);
509
510 return ERR_OK;
511#else
512 LWIP_UNUSED_ARG(idx);
513 LWIP_UNUSED_ARG(context);
514 return ERR_ARG;
515#endif
516}
517
518#if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
519/**
520 * @ingroup sixlowpan
521 * Set short address
522 */
523err_t
524lowpan6_set_short_addr(u8_t addr_high, u8_t addr_low)
525{
526 short_mac_addr.addr[0] = addr_high;
527 short_mac_addr.addr[1] = addr_low;
528
529 return ERR_OK;
530}
531#endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
532
533/* Create IEEE 802.15.4 address from netif address */
534static err_t
535lowpan6_hwaddr_to_addr(struct netif *netif, struct lowpan6_link_addr *addr)
536{
537 addr->addr_len = 8;
538 if (netif->hwaddr_len == 8) {
539 LWIP_ERROR("NETIF_MAX_HWADDR_LEN >= 8 required", sizeof(netif->hwaddr) >= 8, return ERR_VAL;);
540 SMEMCPY(addr->addr, netif->hwaddr, 8);
541 } else if (netif->hwaddr_len == 6) {
542 /* Copy from MAC-48 */
543 SMEMCPY(addr->addr, netif->hwaddr, 3);
544 addr->addr[3] = addr->addr[4] = 0xff;
545 SMEMCPY(&addr->addr[5], &netif->hwaddr[3], 3);
546 } else {
547 /* Invalid address length, don't know how to convert this */
548 return ERR_VAL;
549 }
550 return ERR_OK;
551}
552
553/**
554 * @ingroup sixlowpan
555 * Resolve and fill-in IEEE 802.15.4 address header for outgoing IPv6 packet.
556 *
557 * Perform Header Compression and fragment if necessary.
558 *
559 * @param netif The lwIP network interface which the IP packet will be sent on.
560 * @param q The pbuf(s) containing the IP packet to be sent.
561 * @param ip6addr The IP address of the packet destination.
562 *
563 * @return err_t
564 */
565err_t
566lowpan6_output(struct netif *netif, struct pbuf *q, const ip6_addr_t *ip6addr)
567{
568 err_t result;
569 const u8_t *hwaddr;
570 struct lowpan6_link_addr src, dest;
571#if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
572 ip6_addr_t ip6_src;
573 struct ip6_hdr *ip6_hdr;
574#endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
575
576#if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
577 /* Check if we can compress source address (use aligned copy) */
578 ip6_hdr = (struct ip6_hdr *)q->payload;
579 ip6_addr_copy_from_packed(ip6_src, ip6_hdr->src);
580 ip6_addr_assign_zone(&ip6_src, IP6_UNICAST, netif);
581 if (lowpan6_get_address_mode(&ip6_src, &short_mac_addr) == 3) {
582 src.addr_len = 2;
583 src.addr[0] = short_mac_addr.addr[0];
584 src.addr[1] = short_mac_addr.addr[1];
585 } else
586#endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
587 {
588 result = lowpan6_hwaddr_to_addr(netif, &src);
589 if (result != ERR_OK) {
590 MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
591 return result;
592 }
593 }
594
595 /* multicast destination IP address? */
596 if (ip6_addr_ismulticast(ip6addr)) {
597 MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
598 /* We need to send to the broadcast address.*/
599 return lowpan6_frag(netif, q, &src, &ieee_802154_broadcast);
600 }
601
602 /* We have a unicast destination IP address */
603 /* @todo anycast? */
604
605#if LWIP_6LOWPAN_INFER_SHORT_ADDRESS
606 if (src.addr_len == 2) {
607 /* If source address was compressable to short_mac_addr, and dest has same subnet and
608 * is also compressable to 2-bytes, assume we can infer dest as a short address too. */
609 dest.addr_len = 2;
610 dest.addr[0] = ((u8_t *)q->payload)[38];
611 dest.addr[1] = ((u8_t *)q->payload)[39];
612 if ((src.addr_len == 2) && (ip6_addr_netcmp_zoneless(&ip6_hdr->src, &ip6_hdr->dest)) &&
613 (lowpan6_get_address_mode(ip6addr, &dest) == 3)) {
614 MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
615 return lowpan6_frag(netif, q, &src, &dest);
616 }
617 }
618#endif /* LWIP_6LOWPAN_INFER_SHORT_ADDRESS */
619
620 /* Ask ND6 what to do with the packet. */
621 result = nd6_get_next_hop_addr_or_queue(netif, q, ip6addr, &hwaddr);
622 if (result != ERR_OK) {
623 MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
624 return result;
625 }
626
627 /* If no hardware address is returned, nd6 has queued the packet for later. */
628 if (hwaddr == NULL) {
629 return ERR_OK;
630 }
631
632 /* Send out the packet using the returned hardware address. */
633 dest.addr_len = netif->hwaddr_len;
634 /* XXX: Inferring the length of the source address from the destination address
635 * is not correct for IEEE 802.15.4, but currently we don't get this information
636 * from the neighbor cache */
637 SMEMCPY(dest.addr, hwaddr, netif->hwaddr_len);
638 MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
639 return lowpan6_frag(netif, q, &src, &dest);
640}
641/**
642 * @ingroup sixlowpan
643 * NETIF input function: don't free the input pbuf when returning != ERR_OK!
644 */
645err_t
646lowpan6_input(struct pbuf *p, struct netif *netif)
647{
648 u8_t *puc, b;
649 s8_t i;
650 struct lowpan6_link_addr src, dest;
651 u16_t datagram_size = 0;
652 u16_t datagram_offset, datagram_tag;
653 struct lowpan6_reass_helper *lrh, *lrh_next, *lrh_prev = NULL;
654
655 if (p == NULL) {
656 return ERR_OK;
657 }
658
659 MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
660
661 if (p->len != p->tot_len) {
662 /* for now, this needs a pbuf in one piece */
663 goto lowpan6_input_discard;
664 }
665
666 if (lowpan6_parse_iee802154_header(p, &src, &dest) != ERR_OK) {
667 goto lowpan6_input_discard;
668 }
669
670 /* Check dispatch. */
671 puc = (u8_t *)p->payload;
672
673 b = *puc;
674 if ((b & 0xf8) == 0xc0) {
675 /* FRAG1 dispatch. add this packet to reassembly list. */
676 datagram_size = ((u16_t)(puc[0] & 0x07) << 8) | (u16_t)puc[1];
677 datagram_tag = ((u16_t)puc[2] << 8) | (u16_t)puc[3];
678
679 /* check for duplicate */
680 lrh = lowpan6_data.reass_list;
681 while (lrh != NULL) {
682 uint8_t discard = 0;
683 lrh_next = lrh->next_packet;
684 if ((lrh->sender_addr.addr_len == src.addr_len) &&
685 (memcmp(lrh->sender_addr.addr, src.addr, src.addr_len) == 0)) {
686 /* address match with packet in reassembly. */
687 if ((datagram_tag == lrh->datagram_tag) && (datagram_size == lrh->datagram_size)) {
688 /* duplicate fragment. */
689 goto lowpan6_input_discard;
690 } else {
691 /* We are receiving the start of a new datagram. Discard old one (incomplete). */
692 discard = 1;
693 }
694 }
695 if (discard) {
696 dequeue_datagram(lrh, lrh_prev);
697 free_reass_datagram(lrh);
698 } else {
699 lrh_prev = lrh;
700 }
701 /* Check next datagram in queue. */
702 lrh = lrh_next;
703 }
704
705 pbuf_remove_header(p, 4); /* hide frag1 dispatch */
706
707 lrh = (struct lowpan6_reass_helper *) mem_malloc(sizeof(struct lowpan6_reass_helper));
708 if (lrh == NULL) {
709 goto lowpan6_input_discard;
710 }
711
712 lrh->sender_addr.addr_len = src.addr_len;
713 for (i = 0; i < src.addr_len; i++) {
714 lrh->sender_addr.addr[i] = src.addr[i];
715 }
716 lrh->datagram_size = datagram_size;
717 lrh->datagram_tag = datagram_tag;
718 lrh->frags = NULL;
719 if (*(u8_t *)p->payload == 0x41) {
720 /* This is a complete IPv6 packet, just skip dispatch byte. */
721 pbuf_remove_header(p, 1); /* hide dispatch byte. */
722 lrh->reass = p;
723 } else if ((*(u8_t *)p->payload & 0xe0 ) == 0x60) {
724 lrh->reass = lowpan6_decompress(p, datagram_size, LWIP_6LOWPAN_CONTEXTS(netif), &src, &dest);
725 if (lrh->reass == NULL) {
726 /* decompression failed */
727 mem_free(lrh);
728 goto lowpan6_input_discard;
729 }
730 }
731 /* TODO: handle the case where we already have FRAGN received */
732 lrh->next_packet = lowpan6_data.reass_list;
733 lrh->timer = 2;
734 lowpan6_data.reass_list = lrh;
735
736 return ERR_OK;
737 } else if ((b & 0xf8) == 0xe0) {
738 /* FRAGN dispatch, find packet being reassembled. */
739 datagram_size = ((u16_t)(puc[0] & 0x07) << 8) | (u16_t)puc[1];
740 datagram_tag = ((u16_t)puc[2] << 8) | (u16_t)puc[3];
741 datagram_offset = (u16_t)puc[4] << 3;
742 pbuf_remove_header(p, 4); /* hide frag1 dispatch but keep datagram offset for reassembly */
743
744 for (lrh = lowpan6_data.reass_list; lrh != NULL; lrh_prev = lrh, lrh = lrh->next_packet) {
745 if ((lrh->sender_addr.addr_len == src.addr_len) &&
746 (memcmp(lrh->sender_addr.addr, src.addr, src.addr_len) == 0) &&
747 (datagram_tag == lrh->datagram_tag) &&
748 (datagram_size == lrh->datagram_size)) {
749 break;
750 }
751 }
752 if (lrh == NULL) {
753 /* rogue fragment */
754 goto lowpan6_input_discard;
755 }
756 /* Insert new pbuf into list of fragments. Each fragment is a pbuf,
757 this only works for unchained pbufs. */
758 LWIP_ASSERT("p->next == NULL", p->next == NULL);
759 if (lrh->reass != NULL) {
760 /* FRAG1 already received, check this offset against first len */
761 if (datagram_offset < lrh->reass->len) {
762 /* fragment overlap, discard old fragments */
763 dequeue_datagram(lrh, lrh_prev);
764 free_reass_datagram(lrh);
765 goto lowpan6_input_discard;
766 }
767 }
768 if (lrh->frags == NULL) {
769 /* first FRAGN */
770 lrh->frags = p;
771 } else {
772 /* find the correct place to insert */
773 struct pbuf *q, *last;
774 u16_t new_frag_len = p->len - 1; /* p->len includes datagram_offset byte */
775 for (q = lrh->frags, last = NULL; q != NULL; last = q, q = q->next) {
776 u16_t q_datagram_offset = ((u8_t *)q->payload)[0] << 3;
777 u16_t q_frag_len = q->len - 1;
778 if (datagram_offset < q_datagram_offset) {
779 if (datagram_offset + new_frag_len > q_datagram_offset) {
780 /* overlap, discard old fragments */
781 dequeue_datagram(lrh, lrh_prev);
782 free_reass_datagram(lrh);
783 goto lowpan6_input_discard;
784 }
785 /* insert here */
786 break;
787 } else if (datagram_offset == q_datagram_offset) {
788 if (q_frag_len != new_frag_len) {
789 /* fragment mismatch, discard old fragments */
790 dequeue_datagram(lrh, lrh_prev);
791 free_reass_datagram(lrh);
792 goto lowpan6_input_discard;
793 }
794 /* duplicate, ignore */
795 pbuf_free(p);
796 return ERR_OK;
797 }
798 }
799 /* insert fragment */
800 if (last == NULL) {
801 lrh->frags = p;
802 } else {
803 last->next = p;
804 p->next = q;
805 }
806 }
807 /* check if all fragments were received */
808 if (lrh->reass) {
809 u16_t offset = lrh->reass->len;
810 struct pbuf *q;
811 for (q = lrh->frags; q != NULL; q = q->next) {
812 u16_t q_datagram_offset = ((u8_t *)q->payload)[0] << 3;
813 if (q_datagram_offset != offset) {
814 /* not complete, wait for more fragments */
815 return ERR_OK;
816 }
817 offset += q->len - 1;
818 }
819 if (offset == datagram_size) {
820 /* all fragments received, combine pbufs */
821 u16_t datagram_left = datagram_size - lrh->reass->len;
822 for (q = lrh->frags; q != NULL; q = q->next) {
823 /* hide datagram_offset byte now */
824 pbuf_remove_header(q, 1);
825 q->tot_len = datagram_left;
826 datagram_left -= q->len;
827 }
828 LWIP_ASSERT("datagram_left == 0", datagram_left == 0);
829 q = lrh->reass;
830 q->tot_len = datagram_size;
831 q->next = lrh->frags;
832 lrh->frags = NULL;
833 lrh->reass = NULL;
834 dequeue_datagram(lrh, lrh_prev);
835 mem_free(lrh);
836
837 /* @todo: distinguish unicast/multicast */
838 MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
839 return ip6_input(q, netif);
840 }
841 }
842 /* pbuf enqueued, waiting for more fragments */
843 return ERR_OK;
844 } else {
845 if (b == 0x41) {
846 /* This is a complete IPv6 packet, just skip dispatch byte. */
847 pbuf_remove_header(p, 1); /* hide dispatch byte. */
848 } else if ((b & 0xe0 ) == 0x60) {
849 /* IPv6 headers are compressed using IPHC. */
850 p = lowpan6_decompress(p, datagram_size, LWIP_6LOWPAN_CONTEXTS(netif), &src, &dest);
851 if (p == NULL) {
852 MIB2_STATS_NETIF_INC(netif, ifindiscards);
853 return ERR_OK;
854 }
855 } else {
856 goto lowpan6_input_discard;
857 }
858
859 /* @todo: distinguish unicast/multicast */
860 MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
861
862 return ip6_input(p, netif);
863 }
864lowpan6_input_discard:
865 MIB2_STATS_NETIF_INC(netif, ifindiscards);
866 pbuf_free(p);
867 /* always return ERR_OK here to prevent the caller freeing the pbuf */
868 return ERR_OK;
869}
870
871/**
872 * @ingroup sixlowpan
873 */
874err_t
875lowpan6_if_init(struct netif *netif)
876{
877 netif->name[0] = 'L';
878 netif->name[1] = '6';
879 netif->output_ip6 = lowpan6_output;
880
881 MIB2_INIT_NETIF(netif, snmp_ifType_other, 0);
882
883 /* maximum transfer unit */
884 netif->mtu = 1280;
885
886 /* broadcast capability */
887 netif->flags = NETIF_FLAG_BROADCAST /* | NETIF_FLAG_LOWPAN6 */;
888
889 return ERR_OK;
890}
891
892/**
893 * @ingroup sixlowpan
894 * Set PAN ID
895 */
896err_t
897lowpan6_set_pan_id(u16_t pan_id)
898{
899 lowpan6_data.ieee_802154_pan_id = pan_id;
900
901 return ERR_OK;
902}
903
904#if !NO_SYS
905/**
906 * @ingroup sixlowpan
907 * Pass a received packet to tcpip_thread for input processing
908 *
909 * @param p the received packet, p->payload pointing to the
910 * IEEE 802.15.4 header.
911 * @param inp the network interface on which the packet was received
912 */
913err_t
914tcpip_6lowpan_input(struct pbuf *p, struct netif *inp)
915{
916 return tcpip_inpkt(p, inp, lowpan6_input);
917}
918#endif /* !NO_SYS */
919
920#endif /* LWIP_IPV6 */
Note: See TracBrowser for help on using the repository browser.