Main uIPv6 files addition - more

This commit is contained in:
julienabeille 2008-10-14 09:42:33 +00:00
parent f95cdc5abb
commit 2e8264010b
10 changed files with 6832 additions and 0 deletions

1423
core/net/sicslowpan.c Normal file

File diff suppressed because it is too large Load Diff

259
core/net/sicslowpan.h Normal file
View File

@ -0,0 +1,259 @@
/**
* \addtogroup sicslowpan
* @{
*/
/*
* Copyright (c) 2008, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* $Id: sicslowpan.h,v 1.1 2008/10/14 09:42:33 julienabeille Exp $
*/
/**
* \file
* Header file for the 6lowpan implementation
* (RFC4944 and draft-hui-6lowpan-hc-01)
* \author Adam Dunkels <adam@sics.se>
* \author Nicolas Tsiftes <nvt@sics.se>
* \author Niclas Finne <nfi@sics.se>
* \author Mathilde Durvy <mdurvy@cisco.com>
* \author Julien Abeille <jabeille@cisco.com>
*/
#ifndef __SICSLOWPAN_H__
#define __SICSLOWPAN_H__
#include "net/uip.h"
#include "sicslowmac.h"
/**
* \name General sicslowpan defines
* @{
*/
/* Min and Max compressable UDP ports */
#define SICSLOWPAN_UDP_PORT_MIN 0xF0B0
#define SICSLOWPAN_UDP_PORT_MAX 0xF0BF /* F0B0 + 15 */
/** @} */
/**
* \name 6lowpan dispatchs
* @{
*/
#define SICSLOWPAN_DISPATCH_IPV6 0x41 /* 01000001 = 65 */
#define SICSLOWPAN_DISPATCH_HC1 0x42 /* 01000010 = 66 */
#define SICSLOWPAN_DISPATCH_IPHC 0x03 /* 00000011 = 3 */
#define SICSLOWPAN_DISPATCH_FRAG1 0xc0 /* 11000xxx */
#define SICSLOWPAN_DISPATCH_FRAGN 0xe0 /* 11100xxx */
/** @} */
/** \name HC1 encoding
* @{
*/
#define SICSLOWPAN_HC1_NH_UDP 0x02
#define SICSLOWPAN_HC1_NH_TCP 0x06
#define SICSLOWPAN_HC1_NH_ICMP6 0x04
/** @} */
/** \name HC_UDP encoding (works together with HC1)
* @{
*/
#define SICSLOWPAN_HC_UDP_ALL_C 0xE0
/** @} */
/**
* \name IPHC encoding
* @{
*/
/*
* Values of fields within the IPHC encoding first byte
* (C stands for compressed and I for inline)
*/
#define SICSLOWPAN_IPHC_TC_C 0x80
#define SICSLOWPAN_IPHC_VF_C 0x40
#define SICSLOWPAN_IPHC_NH_C 0x20
#define SICSLOWPAN_IPHC_TTL_1 0x08
#define SICSLOWPAN_IPHC_TTL_64 0x10
#define SICSLOWPAN_IPHC_TTL_255 0x18
#define SICSLOWPAN_IPHC_TTL_I 0x00
/* Values of fields within the IPHC encoding second byte */
#define SICSLOWPAN_IPHC_SAM_I 0x00
#define SICSLOWPAN_IPHC_SAM_64 0x40
#define SICSLOWPAN_IPHC_SAM_16 0x80
#define SICSLOWPAN_IPHC_SAM_0 0xC0
#define SICSLOWPAN_IPHC_DAM_I 0x00
#define SICSLOWPAN_IPHC_DAM_64 0x04
#define SICSLOWPAN_IPHC_DAM_16 0x08
#define SICSLOWPAN_IPHC_DAM_0 0x0C
/* Link local context number */
#define SICSLOWPAN_IPHC_ADDR_CONTEXT_LL 0
/* 16-bit multicast addresses compression */
#define SICSLOWPAN_IPHC_MCAST_RANGE 0xA0
/** @} */
/**
* \name LOWPAN_UDP encoding (works together with IPHC)
* @{
*/
#define SICSLOWPAN_NHC_UDP_ID 0xF8
#define SICSLOWPAN_NHC_UDP_C 0xFB
#define SICSLOWPAN_NHC_UDP_I 0xF8
/** @} */
/**
* \name The 6lowpan "headers" length
* @{
*/
#define SICSLOWPAN_IPV6_HDR_LEN 1 /*one byte*/
#define SICSLOWPAN_HC1_HDR_LEN 3
#define SICSLOWPAN_HC1_HC_UDP_HDR_LEN 7
#define SICSLOWPAN_FRAG1_HDR_LEN 4
#define SICSLOWPAN_FRAGN_HDR_LEN 5
/** @} */
/**
* \brief The header for fragments
* \note We do not define different structuresfor FRAG1
* and FRAGN headers, which are different. For FRAG1, the
* offset field is just not used
*/
struct sicslowpan_frag_hdr {
u16_t dispatch_size;
u16_t tag;
u8_t offset;
};
/**
* \brief The HC1 header when HC_UDP is not used
*
* When all fields are compressed and HC_UDP is not used,
* we use this structure. If HC_UDP is used, the ttl is
* in another spot, and we use the sicslowpan_hc1_hc_udp
* structure
*/
struct sicslowpan_hc1_hdr {
u8_t dispatch;
u8_t encoding;
u8_t ttl;
};
/**
* \brief HC1 followed by HC_UDP
*/
struct sicslowpan_hc1_hc_udp_hdr {
u8_t dispatch;
u8_t hc1_encoding;
u8_t hc_udp_encoding;
u8_t ttl;
u8_t ports;
u16_t udpchksum;
};
/**
* \brief IPHC dispatch and encoding
* the rest (uncompressed fields) is variable
*/
struct sicslowpan_iphc_hdr {
u8_t dispatch;
u8_t encoding[2];
};
struct sicslowpan_nhc_udp_comp_hdr {
u8_t nhcid;
u8_t ports;
u16_t udpchksum;
};
/**
* \brief An address context for IPHC address compression
*/
struct sicslowpan_addr_context {
u8_t used;
u8_t number;
u8_t prefix[8];
};
/**
* \name Address compressibility test functions
* @{
*/
/**
* \brief check whether we can compress the IID in
* address 'a' to 16 bits.
* This is used for unicast addresses only, and is true
* if first 49 bits of IID are 0
*/
#define sicslowpan_is_iid_16_bit_compressable(a) \
((((a)->u16[4]) == 0) && \
(((a)->u16[5]) == 0) && \
(((a)->u16[6]) == 0) && \
((((a)->u8[14]) & 0x80) == 0))
/**
* \brief check whether the 9-bit group-id of the
* compressed multicast address is known. It is true
* if the 9-bit group is the all nodes or all routers
* group.
* \param a is typed u8_t *
*/
#define sicslowpan_is_mcast_addr_decompressable(a) \
(((*a & 0x01) == 0) && \
((*(a + 1) == 0x01) || (*(a + 1) == 0x02)))
/**
* \brief check whether the 112-bit group-id of the
* multicast address is mapable to a 9-bit group-id
* It is true if the group is the all nodes or all
* routers group.
*/
#define sicslowpan_is_mcast_addr_compressable(a) \
((((a)->u16[1]) == 0) && \
(((a)->u16[2]) == 0) && \
(((a)->u16[3]) == 0) && \
(((a)->u16[4]) == 0) && \
(((a)->u16[5]) == 0) && \
(((a)->u16[6]) == 0) && \
(((a)->u8[14]) == 0) && \
((((a)->u8[15]) == 1) || (((a)->u8[15]) == 2)))
/** @} */
/**
* \brief 6lowpan init function
* \param m is the MAC layer "driver"
*/
void sicslowpan_init(const struct mac_driver *m);
#endif /* __SICSLOWPAN_H__ */
/** @} */

185
core/net/uip-icmp6.c Normal file
View File

@ -0,0 +1,185 @@
/**
* \addtogroup uip6
* @{
*/
/**
* \file
* ICMPv6 echo request and error messages (RFC 4443)
* \author Julien Abeille <jabeille@cisco.com>
* \author Mathilde Durvy <mdurvy@cisco.com>
*/
/*
* Copyright (c) 2001-2003, Adam Dunkels.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file is part of the uIP TCP/IP stack.
*
*/
#include <string.h>
#include "net/uip-netif.h"
#include "net/uip-icmp6.h"
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",lladdr->addr[0], lladdr->addr[1], lladdr->addr[2], lladdr->addr[3],lladdr->addr[4], lladdr->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#endif
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
#define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)&uip_buf[uip_l2_l3_icmp_hdr_len])
/** \brief temporary IP address */
static uip_ipaddr_t tmp_ipaddr;
void
uip_icmp6_echo_request_input(void)
{
/*
* we send an echo reply. It is trivial if there was no extension
* headers in the request otherwise we need to remove the extension
* headers and change a few fields
*/
PRINTF("Received Echo Request from");
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
PRINTF("to");
PRINT6ADDR(&UIP_IP_BUF->destipaddr);
PRINTF("\n");
/* IP header */
UIP_IP_BUF->ttl = uip_netif_physical_if.cur_hop_limit;
if(uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)){
uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
uip_netif_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
} else {
uip_ipaddr_copy(&tmp_ipaddr, &UIP_IP_BUF->srcipaddr);
uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &tmp_ipaddr);
}
if(uip_ext_len > 0) {
/* If there were extension headers*/
UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
uip_len -= uip_ext_len;
UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
/* move the echo request payload (starting after the icmp header)
* to the new location in the reply.
* The shift is equal to the length of the extension headers present
* Note: UIP_ICMP_BUF still points to the echo request at this stage
*/
memmove((void *)UIP_ICMP_BUF + UIP_ICMPH_LEN - uip_ext_len,
(void *)UIP_ICMP_BUF + UIP_ICMPH_LEN,
(uip_len - UIP_IPH_LEN - UIP_ICMPH_LEN));
}
/* Below is important for the correctness of UIP_ICMP_BUF and the
* checksum
*/
uip_ext_len = 0;
/* Note: now UIP_ICMP_BUF points to the beginning of the echo reply */
UIP_ICMP_BUF->type = ICMP6_ECHO_REPLY;
UIP_ICMP_BUF->icode = 0;
UIP_ICMP_BUF->icmpchksum = 0;
UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
PRINTF("Sending Echo Reply to");
PRINT6ADDR(&UIP_IP_BUF->destipaddr);
PRINTF("from");
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
PRINTF("\n");
UIP_STAT(++uip_stat.icmp.sent);
return;
}
void
uip_icmp6_error_output(u8_t type, u8_t code, u32_t param) {
uip_ext_len = 0;
/* remember data of original packet before shifting */
uip_ipaddr_copy(&tmp_ipaddr, &UIP_IP_BUF->destipaddr);
uip_len += UIP_IPICMPH_LEN + UIP_ICMP6_ERROR_LEN;
if(uip_len > UIP_LINK_MTU)
uip_len = UIP_LINK_MTU;
memmove((void *)UIP_ICMP6_ERROR_BUF + UIP_ICMP6_ERROR_LEN,
(void *)UIP_IP_BUF, uip_len - UIP_IPICMPH_LEN - UIP_ICMP6_ERROR_LEN);
UIP_IP_BUF->vtc = 0x60;
UIP_IP_BUF->tcflow = 0;
UIP_IP_BUF->flow = 0;
UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
UIP_IP_BUF->ttl = uip_netif_physical_if.cur_hop_limit;
/* the source should not be unspecified nor multicast, the check for
multicast is done in uip_process */
if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)){
uip_len = 0;
return;
}
uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
if(uip_is_addr_mcast(&tmp_ipaddr)){
if(type == ICMP6_PARAM_PROB && code == ICMP6_PARAMPROB_OPTION){
uip_netif_select_src(&UIP_IP_BUF->srcipaddr, &tmp_ipaddr);
} else {
uip_len = 0;
return;
}
} else {
uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &tmp_ipaddr);
}
UIP_ICMP_BUF->type = type;
UIP_ICMP_BUF->icode = code;
UIP_ICMP6_ERROR_BUF->param = htonl(param);
UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
UIP_ICMP_BUF->icmpchksum = 0;
UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
UIP_STAT(++uip_stat.icmp.sent);
PRINTF("Sending ICMPv6 ERROR message to");
PRINT6ADDR(&UIP_IP_BUF->destipaddr);
PRINTF("from");
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
PRINTF("\n");
return;
}
/** @} */

127
core/net/uip-icmp6.h Normal file
View File

@ -0,0 +1,127 @@
/**
* \addtogroup uip6
* @{
*/
/**
* \file
* ICMPv6 echo request and error messages (RFC 4443)
* \author Julien Abeille <jabeille@cisco.com>
* \author Mathilde Durvy <mdurvy@cisco.com>
*/
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
*/
#ifndef __ICMP6_H__
#define __ICMP6_H__
#include "net/uip.h"
/** \name ICMPv6 message types */
/** @{ */
#define ICMP6_DST_UNREACH 1 /**< dest unreachable */
#define ICMP6_PACKET_TOO_BIG 2 /**< packet too big */
#define ICMP6_TIME_EXCEEDED 3 /**< time exceeded */
#define ICMP6_PARAM_PROB 4 /**< ip6 header bad */
#define ICMP6_ECHO_REQUEST 128 /**< Echo request */
#define ICMP6_ECHO_REPLY 129 /**< Echo reply */
#define ICMP6_RS 133 /**< Router Solicitation */
#define ICMP6_RA 134 /**< Router Advertisement */
#define ICMP6_NS 135 /**< Neighbor Solicitation */
#define ICMP6_NA 136 /**< Neighbor advertisement */
#define ICMP6_REDIRECT 137 /**< Redirect */
/** @} */
/** \name ICMPv6 Destination Unreachable message codes*/
/** @{ */
#define ICMP6_DST_UNREACH_NOROUTE 0 /**< no route to destination */
#define ICMP6_DST_UNREACH_ADMIN 1 /**< administratively prohibited */
#define ICMP6_DST_UNREACH_NOTNEIGHBOR 2 /**< not a neighbor(obsolete) */
#define ICMP6_DST_UNREACH_BEYONDSCOPE 2 /**< beyond scope of source address */
#define ICMP6_DST_UNREACH_ADDR 3 /**< address unreachable */
#define ICMP6_DST_UNREACH_NOPORT 4 /**< port unreachable */
/** @} */
/** \name ICMPv6 Time Exceeded message codes*/
/** @{ */
#define ICMP6_TIME_EXCEED_TRANSIT 0 /**< ttl==0 in transit */
#define ICMP6_TIME_EXCEED_REASSEMBLY 1 /**< ttl==0 in reass */
/** @} */
/** \name ICMPv6 Parameter Problem message codes*/
/** @{ */
#define ICMP6_PARAMPROB_HEADER 0 /**< erroneous header field */
#define ICMP6_PARAMPROB_NEXTHEADER 1 /**< unrecognized next header */
#define ICMP6_PARAMPROB_OPTION 2 /**< unrecognized option */
/** @} */
/** \brief Echo Request constant part length */
#define UIP_ICMP6_ECHO_REQUEST_LEN 4
/** \brief ICMPv6 Error message constant part length */
#define UIP_ICMP6_ERROR_LEN 4
/** \brief ICMPv6 Error message constant part */
struct uip_icmp6_error{
u32_t param;
};
/** \name ICMPv6 RFC4443 Message processing and sending */
/** @{ */
/** \
* brief Process an echo request
*
* Perform a few checks, then send an Echop reply. The reply is
* built here.
*/
void
uip_icmp6_echo_request_input(void);
/**
* \brief Send an icmpv6 error message
* \param type type of the Error message
* \param code of the error message
* \param type 32 bit parameter of the error message, semantic depends on error
*/
void
uip_icmp6_error_output(u8_t type, u8_t code, u32_t param);
/** @} */
#endif /*__ICMP6_H__*/
/** @} */

919
core/net/uip-nd6-io.c Normal file
View File

@ -0,0 +1,919 @@
/**
* \addtogroup uip6
* @{
*/
/**
* \file
* Neighbor discovery Input-Output (RFC 4861)
* \author Julien Abeille <jabeille@cisco.com>
* \author Mathilde Durvy <mdurvy@cisco.com>
*/
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <string.h>
#include "net/uip-icmp6.h"
#include "net/uip-nd6.h"
#include "net/uip-netif.h"
/*------------------------------------------------------------------*/
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",lladdr->addr[0], lladdr->addr[1], lladdr->addr[2], lladdr->addr[3],lladdr->addr[4], lladdr->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(lladdr)
#endif
#if UIP_LOGGING
#include <stdio.h>
void uip_log(char *msg);
#define UIP_LOG(m) uip_log(m)
#else
#define UIP_LOG(m)
#endif /* UIP_LOGGING == 1 */
/*------------------------------------------------------------------*/
/** @{ */
/** \name Pointers to the header structures.
* All pointers except UIP_IP_BUF depend on uip_ext_len, which at
* packet reception, is the total length of the extension headers.
*
* The pointer to ND6 options header also depends on nd6_opt_offset,
* which we set in each function.
*
* Care should be taken when manipulating these buffers about the
* value of these length variables
*/
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
/**< Pointer to IP header */
#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
/**< Pointer to ICMP header*/
/**@{ Pointers to messages just after icmp header */
#define UIP_ND6_RS_BUF ((struct uip_nd6_rs *)&uip_buf[uip_l2_l3_icmp_hdr_len])
#define UIP_ND6_RA_BUF ((struct uip_nd6_ra *)&uip_buf[uip_l2_l3_icmp_hdr_len])
#define UIP_ND6_NS_BUF ((struct uip_nd6_ns *)&uip_buf[uip_l2_l3_icmp_hdr_len])
#define UIP_ND6_NA_BUF ((struct uip_nd6_na *)&uip_buf[uip_l2_l3_icmp_hdr_len])
#define UIP_ICMP6_ERROR_BUF ((struct uip_icmp6_error *)&uip_buf[uip_l2_l3_icmp_hdr_len])
/** @} */
/** Pointer to ND option */
#define UIP_ND6_OPT_HDR_BUF ((struct uip_nd6_opt_hdr *)&uip_buf[uip_l2_l3_icmp_hdr_len + nd6_opt_offset])
/** @} */
/** \brief Offset from the end of the icmpv6 header to the option in uip_buf*/
static u8_t nd6_opt_offset;
/** \brief Pointer to llao option in uip_buf */
static struct uip_nd6_opt_llao *nd6_opt_llao;
/** \brief Pointer to mtu option in uip_buf */
static struct uip_nd6_opt_mtu *nd6_opt_mtu;
/** \brief Pointer to prefix information option in uip_buf */
static struct uip_nd6_opt_prefix_info *nd6_opt_prefix_info[UIP_CONF_ND6_MAX_PREFIXES];
/** \brief Pointer to a neighbor cache entry*/
static struct uip_nd6_neighbor *neighbor;
/** \brief Pointer to a prefix list entry */
static struct uip_nd6_prefix *prefix;
/** \brief Pointer to a router list entry */
static struct uip_nd6_defrouter *router;
/** \brief Pointer to an interface address */
static struct uip_netif_addr *ifaddr;
/** \brief Index used in loops */
static u8_t i;
/*------------------------------------------------------------------*/
void
uip_nd6_io_ns_input(void)
{
PRINTF("Received NS from");
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
PRINTF("to");
PRINT6ADDR(&UIP_IP_BUF->destipaddr);
PRINTF("with target address");
PRINT6ADDR((uip_ipaddr_t *)(&UIP_ND6_NS_BUF->tgtipaddr));
PRINTF("\n");
UIP_STAT(++uip_stat.nd6.recv);
u8_t flags;
#if UIP_CONF_IPV6_CHECKS
if((UIP_IP_BUF->ttl != UIP_ND6_HOP_LIMIT) ||
(uip_is_addr_mcast(&UIP_ND6_NS_BUF->tgtipaddr)) ||
(UIP_ICMP_BUF->icode != 0))
{
goto badpkt;
}
#endif /* UIP_CONF_IPV6_CHECKS */
/* Options reading: we handle only SLLAO for now */
nd6_opt_llao = NULL;
nd6_opt_offset = UIP_ND6_NS_LEN;
while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) {
#if UIP_CONF_IPV6_CHECKS
if(UIP_ND6_OPT_HDR_BUF->len == 0) {
goto badpkt;
}
#endif /* UIP_CONF_IPV6_CHECKS */
switch(UIP_ND6_OPT_HDR_BUF->type) {
case UIP_ND6_OPT_SLLAO:
nd6_opt_llao = (struct uip_nd6_opt_llao *)UIP_ND6_OPT_HDR_BUF;
break;
default:
UIP_LOG("ND option not supported in NS");
break;
}
nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3);
}
/* Options processing: only SLLAO */
if(nd6_opt_llao != NULL) {
#if UIP_CONF_IPV6_CHECKS
/* There must be NO option in a DAD NS */
if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
goto badpkt;
} else {
#endif /*UIP_CONF_IPV6_CHECKS*/
neighbor = uip_nd6_nbrcache_lookup(&UIP_IP_BUF->srcipaddr);
if(neighbor == NULL) {
/* we need to add the neighbor*/
uip_nd6_nbrcache_add(&UIP_IP_BUF->srcipaddr,
&nd6_opt_llao->addr, 0, STALE);
} else {
/* If LL address changed, set neighbor state to stale */
if(memcmp(&nd6_opt_llao->addr, &neighbor->lladdr, UIP_LLADDR_LEN) != 0) {
memcpy(&neighbor->lladdr, &nd6_opt_llao->addr, UIP_LLADDR_LEN);
neighbor->state = STALE;
} else {
/* If neighbor state is INCOMPLETE, set to STALE */
if(neighbor->state == INCOMPLETE) {
neighbor->state = STALE;
}
}
}
#if UIP_CONF_IPV6_CHECKS
}
#endif /*UIP_CONF_IPV6_CHECKS*/
}
/*
* Rest of NS processing: Depends on the purpose of the NS: NUD or DAD or
* Address Resolution
*/
/** \note we use ifaddr to remember the target address */
ifaddr = uip_netif_addr_lookup(&UIP_ND6_NS_BUF->tgtipaddr, 128, 0);
if(ifaddr != NULL) {
if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)){
/* DAD CASE */
#if UIP_CONF_IPV6_CHECKS
/* Dst address must be solicited node mcast address */
if(!uip_netif_is_addr_my_solicited(&UIP_IP_BUF->destipaddr)){
goto badpkt;
}
#endif /* UIP_CONF_IPV6_CHECKS */
/*
* If my address is not tentative, then send a NA to all nodes with
* TLLAO flags are: override = yes.
*/
if(ifaddr->state!=TENTATIVE) {
/*
* we need to send a NA, we set the src, dest, flags. tgt remains the
* same and the rest is done at "create_na"
*/
uip_create_linklocal_allnodes_mcast(&UIP_IP_BUF->destipaddr);
uip_netif_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
flags = UIP_ND6_NA_FLAG_OVERRIDE;
goto create_na;
} else {
/** \todo if I sent a NS before him, I win */
uip_netif_dad_failed(&UIP_ND6_NS_BUF->tgtipaddr);
goto discard;
}
}
#if UIP_CONF_IPV6_CHECKS
/* Duplicate check */
if(uip_netif_is_addr_my_unicast(&UIP_IP_BUF->srcipaddr)) {
/**
* \NOTE do we do something here? we both are using the same address.
* If we are doing dad, we could cancel it, though we should receive a
* NA in response of DAD NS we sent, hence DAD will fail anyway. If we
* were not doing DAD, it means there is a duplicate in the network!
*/
goto badpkt;
}
#endif /*UIP_CONF_IPV6_CHECKS*/
/* Address resolution case */
if(uip_netif_is_addr_my_solicited(&UIP_IP_BUF->destipaddr)){
/*
* we need to send a NA, we set the src, dest, flags. The rest is
* set at the "create_na" label.
*/
uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &UIP_ND6_NS_BUF->tgtipaddr);
flags = UIP_ND6_NA_FLAG_SOLICITED | UIP_ND6_NA_FLAG_OVERRIDE;
goto create_na;
}
/*
* NUD CASE. at this point the packet must be for us! we check this,
* and at the same time if target == dest
*/
if(uip_netif_addr_lookup(&UIP_IP_BUF->destipaddr, 128, 0) == ifaddr){
/*
* we need to send a NA, we set the src, dest, flags. The rest is set
* at the "create_na" label.
*/
uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &UIP_ND6_NS_BUF->tgtipaddr);
flags = UIP_ND6_NA_FLAG_SOLICITED | UIP_ND6_NA_FLAG_OVERRIDE;
goto create_na;
} else {
#if UIP_CONF_IPV6_CHECKS
goto badpkt;
#endif /* UIP_CONF_IPV6_CHECKS */
}
} else {
goto discard;
}
create_na:
/*
* Fill the part of the NA which is common to all NAs. If the NS contained
* extension headers, we must set the target address properly
*/
uip_ext_len = 0;
/* IP header */
UIP_IP_BUF->vtc = 0x60;
UIP_IP_BUF->tcflow = 0;
UIP_IP_BUF->flow = 0;
UIP_IP_BUF->len[0] = 0; /* length will not be more than 255 */
UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN;
UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
UIP_IP_BUF->ttl = UIP_ND6_HOP_LIMIT;
/* ICMP header */
UIP_ICMP_BUF->type = ICMP6_NA;
UIP_ICMP_BUF->icode = 0;
/* NA static part */
UIP_ND6_NA_BUF->flagsreserved = flags;
memcpy(&UIP_ND6_NA_BUF->tgtipaddr, &ifaddr->ipaddr, sizeof(uip_ipaddr_t));
/* NA option: TLLAO. note that length field is in unit of 8 bytes */
uip_len = UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NA_LEN + UIP_ND6_OPT_LLAO_LEN;
nd6_opt_llao = (struct uip_nd6_opt_llao *)&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NA_LEN];
nd6_opt_llao->type = UIP_ND6_OPT_TLLAO;
nd6_opt_llao->len = UIP_ND6_OPT_LLAO_LEN >> 3;
memcpy(&(nd6_opt_llao->addr), &uip_lladdr, UIP_LLADDR_LEN);
/* padding if needed */
memset((void *)(&nd6_opt_llao->addr) + UIP_LLADDR_LEN, 0, UIP_ND6_OPT_LLAO_LEN - 2 - UIP_LLADDR_LEN);
/*ICMP checksum*/
UIP_ICMP_BUF->icmpchksum = 0;
UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
UIP_STAT(++uip_stat.nd6.sent);
PRINTF("Sending NA to");
PRINT6ADDR(&UIP_IP_BUF->destipaddr);
PRINTF("from");
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
PRINTF("with target address");
PRINT6ADDR(&UIP_ND6_NA_BUF->tgtipaddr);
PRINTF("\n");
return;
#if UIP_CONF_IPV6_CHECKS
badpkt:
UIP_STAT(++uip_stat.nd6.drop);
UIP_LOG("NS received is bad");
#endif /* UIP_CONF_IPV6_CHECKS */
discard:
uip_len = 0;
return;
}
/*------------------------------------------------------------------*/
void
uip_nd6_io_ns_output(uip_ipaddr_t *src, uip_ipaddr_t *dest, uip_ipaddr_t *tgt)
{
/* IP header fields */
uip_ext_len = 0;
UIP_IP_BUF->vtc = 0x60;
UIP_IP_BUF->tcflow = 0;
UIP_IP_BUF->flow = 0;
UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
UIP_IP_BUF->ttl = UIP_ND6_HOP_LIMIT;
/*
* The caller must put a valid tgt address.
* For dest, if the caller is doing DAD or Address resolution, he sets
* dest to NULL and we forge dest as the sollicited node mcast address
* for tgt.
* If the caller is sending the NS for NUD, dest is unicast and the caller
* specifies it in the arguments
*/
if(dest == NULL) {
uip_create_solicited_node(tgt, &UIP_IP_BUF->destipaddr);
} else {
uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, dest);
}
UIP_ICMP_BUF->type = ICMP6_NS;
UIP_ICMP_BUF->icode = 0;
UIP_ND6_NS_BUF->reserved = 0;
/* Copy the tgt address */
uip_ipaddr_copy((uip_ipaddr_t *)&UIP_ND6_NS_BUF->tgtipaddr, tgt);
UIP_IP_BUF->len[0] = 0; /* length will not be more than 255 */
/*
* check if we add a SLLAO option: for DAD, MUST NOT, for NUD, MAY
* (here yes), for Address resolution , MUST
* i.e. if and only if tgt is our address (in this case it is DAD), we do
* not add it
*/
if(!(uip_netif_is_addr_my_unicast(tgt))) {
if(src != NULL) {
uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, src);
} else {
uip_netif_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
}
UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_NS_LEN + UIP_ND6_OPT_LLAO_LEN;
uip_len = UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NS_LEN + UIP_ND6_OPT_LLAO_LEN;
nd6_opt_llao = (struct uip_nd6_opt_llao *)&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_NS_LEN];
nd6_opt_llao->type = UIP_ND6_OPT_SLLAO; /* type of the option */
/*
* length of the option: 2 bytes for type, length, plus the length of
* the L2 address. It must be in units of 8 bytes
*/
nd6_opt_llao->len = UIP_ND6_OPT_LLAO_LEN >> 3;
memcpy(&nd6_opt_llao->addr, &uip_lladdr, UIP_LLADDR_LEN);
/* padding if needed */
memset((void *)(&nd6_opt_llao->addr) + UIP_LLADDR_LEN, 0, UIP_ND6_OPT_LLAO_LEN - 2 - UIP_LLADDR_LEN);
}
else {
uip_create_unspecified(&UIP_IP_BUF->srcipaddr);
UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_NS_LEN;
uip_len = UIP_IPH_LEN + UIP_ICMPH_LEN + UIP_ND6_NS_LEN;
}
UIP_ICMP_BUF->icmpchksum = 0;
UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
UIP_STAT(++uip_stat.nd6.sent);
PRINTF("Sending NS to");
PRINT6ADDR(&UIP_IP_BUF->destipaddr);
PRINTF("from");
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
PRINTF("with target address");
PRINT6ADDR(tgt);
PRINTF("\n");
return;
}
/*------------------------------------------------------------------*/
void
uip_nd6_io_na_input(void)
{
PRINTF("Received NA from");
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
PRINTF("to");
PRINT6ADDR(&UIP_IP_BUF->destipaddr);
PRINTF("with target address");
PRINT6ADDR((uip_ipaddr_t *)(&UIP_ND6_NA_BUF->tgtipaddr));
PRINTF("\n");
UIP_STAT(++uip_stat.nd6.recv);
/*
* booleans. the three last one are not 0 or 1 but 0 or 0x80, 0x40, 0x20
* but it works. Be careful though, do not use tests such as is_router == 1
*/
u8_t is_llchange = 0;
u8_t is_router = ((UIP_ND6_NA_BUF->flagsreserved & UIP_ND6_NA_FLAG_ROUTER));
u8_t is_solicited = ((UIP_ND6_NA_BUF->flagsreserved & UIP_ND6_NA_FLAG_SOLICITED));
u8_t is_override = ((UIP_ND6_NA_BUF->flagsreserved & UIP_ND6_NA_FLAG_OVERRIDE));
#if UIP_CONF_IPV6_CHECKS
/*
* Check hop limit / icmp code
* target address must not be multicast
* if the NA is solicited, dest must not be multicast
*/
if((UIP_IP_BUF->ttl != UIP_ND6_HOP_LIMIT) ||
(UIP_ICMP_BUF->icode != 0) ||
(uip_is_addr_mcast(&UIP_ND6_NA_BUF->tgtipaddr)) ||
(is_solicited && uip_is_addr_mcast(&UIP_IP_BUF->destipaddr))) {
goto badpkt;
}
#endif /*UIP_CONF_IPV6_CHECKS*/
/* Options reading: we can handle TLLAO, and must ignore othehrs */
nd6_opt_offset = UIP_ND6_NA_LEN;
nd6_opt_llao = NULL;
while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) {
#if UIP_CONF_IPV6_CHECKS
if(UIP_ND6_OPT_HDR_BUF->len == 0) {
goto badpkt;
}
#endif /*UIP_CONF_IPV6_CHECKS*/
switch(UIP_ND6_OPT_HDR_BUF->type) {
case UIP_ND6_OPT_TLLAO:
nd6_opt_llao = (struct uip_nd6_opt_llao *)UIP_ND6_OPT_HDR_BUF;
break;
default:
UIP_LOG("ND option not supported in NA");
break;
}
nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3);
}
ifaddr = uip_netif_addr_lookup(&UIP_ND6_NA_BUF->tgtipaddr, 128, 0);
/* Message processing, including TLLAO if any */
if(ifaddr != NULL) {
if(ifaddr->state == TENTATIVE) {
/*It means DAD failed*/
uip_netif_dad_failed(&UIP_ND6_NA_BUF->tgtipaddr);
}
goto discard;
} else {
neighbor = uip_nd6_nbrcache_lookup(&UIP_ND6_NA_BUF->tgtipaddr);
/* if the neighbor entry does not exist, silently discard */
if(neighbor == NULL) {
goto discard;
}
if(nd6_opt_llao != 0) {
is_llchange = memcmp((void *)&nd6_opt_llao->addr, (void *)(&neighbor->lladdr), UIP_LLADDR_LEN);
}
if(neighbor->state == INCOMPLETE){
if(nd6_opt_llao == 0) {
goto discard;
}
memcpy(&neighbor->lladdr, &nd6_opt_llao->addr, UIP_LLADDR_LEN);
if(is_solicited) {
neighbor->state = REACHABLE;
/* reachable time is stored in ms*/
timer_set(&(neighbor->reachable),
uip_netif_physical_if.reachable_time * CLOCK_SECOND / 1000);
} else {
neighbor->state = STALE;
}
neighbor->isrouter = is_router;
} else {
if(!is_override && is_llchange) {
if(neighbor->state == REACHABLE) {
neighbor->state = STALE;
}
goto discard;
} else {
if(is_override
|| (!is_override && nd6_opt_llao != 0 && !is_llchange)
|| nd6_opt_llao == 0) {
/* update LL address if any */
if(nd6_opt_llao != 0) {
memcpy(&neighbor->lladdr, &nd6_opt_llao->addr, UIP_LLADDR_LEN);
}
/*
* if the NA was solicited, change the state of the neighbor to
* reachabe. Otherwise and if the neighbor LL changed, set the
* state to STALE
*/
if(is_solicited) {
neighbor->state = REACHABLE;
/* reachable time is stored in ms*/
timer_set(&(neighbor->reachable),
uip_netif_physical_if.reachable_time*CLOCK_SECOND/1000);
} else {
if(nd6_opt_llao != 0 && is_llchange) {
neighbor->state = STALE;
}
}
}
}
if(neighbor->isrouter && !is_router){
/* the neighbor stopped being a router, we remove it from the list */
router = uip_nd6_defrouter_lookup(neighbor);
if(router != NULL){
uip_nd6_defrouter_rm(router);
}
}
neighbor->isrouter = is_router;
}
}
#if UIP_CONF_IPV6_QUEUE_PKT
/* The neighbor is now reachable, check if we had buffered a pkt for it */
if(neighbor->queue_buf_len != 0) {
uip_len = neighbor->queue_buf_len;
memcpy(UIP_IP_BUF, neighbor->queue_buf, uip_len);
neighbor->queue_buf_len = 0;
return;
}
#endif /*UIP_CONF_IPV6_QUEUE_PKT*/
uip_len = 0;
return;
#if UIP_CONF_IPV6_CHECKS
badpkt:
UIP_STAT(++uip_stat.nd6.drop);
UIP_LOG("NA received is bad");
#endif /* UIP_CONF_IPV6_CHECKS */
discard:
uip_len = 0;
return;
}
/*---------------------------------------------------------------------------*/
void
uip_nd6_io_rs_output(void) {
UIP_IP_BUF->vtc = 0x60;
UIP_IP_BUF->tcflow = 0;
UIP_IP_BUF->flow = 0;
UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
UIP_IP_BUF->ttl = UIP_ND6_HOP_LIMIT;
uip_create_linklocal_allrouters_mcast(&UIP_IP_BUF->destipaddr);
uip_netif_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
UIP_ICMP_BUF->type = ICMP6_RS;
UIP_ICMP_BUF->icode = 0;
UIP_ND6_RS_BUF->reserved = 0;
UIP_IP_BUF->len[0] = 0; /* length will not be more than 255 */
/* we add the SLLAO option only if src is not unspecified */
if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)) {
UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_RS_LEN;
uip_len = uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN;
} else {
uip_len = uip_l3_icmp_hdr_len + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN;
UIP_IP_BUF->len[1] = UIP_ICMPH_LEN + UIP_ND6_RS_LEN + UIP_ND6_OPT_LLAO_LEN;
nd6_opt_llao = (struct uip_nd6_opt_llao *)&uip_buf[uip_l2_l3_icmp_hdr_len + UIP_ND6_RS_LEN];
nd6_opt_llao->type = UIP_ND6_OPT_SLLAO; /* type of the option */
nd6_opt_llao->len = UIP_ND6_OPT_LLAO_LEN >> 3;
/* length of the option in units of 8 bytes */
memcpy(&nd6_opt_llao->addr, &uip_lladdr, UIP_LLADDR_LEN);
/* padding if needed */
memset((void *)(&nd6_opt_llao->addr) + UIP_LLADDR_LEN, 0, UIP_ND6_OPT_LLAO_LEN - 2 - UIP_LLADDR_LEN);
}
UIP_ICMP_BUF->icmpchksum = 0;
UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
UIP_STAT(++uip_stat.nd6.sent);
PRINTF("Sendin RS to");
PRINT6ADDR(&UIP_IP_BUF->destipaddr);
PRINTF("from");
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
PRINTF("\n");
return;
}
/*---------------------------------------------------------------------------*/
void
uip_nd6_io_ra_input(void) {
PRINTF("Received RA from");
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
PRINTF("to");
PRINT6ADDR(&UIP_IP_BUF->destipaddr);
PRINTF("\n");
UIP_STAT(++uip_stat.nd6.recv);
#if UIP_CONF_IPV6_CHECKS
/* a few validity checks */
if((UIP_IP_BUF->ttl != UIP_ND6_HOP_LIMIT) ||
(!uip_is_addr_link_local(&UIP_IP_BUF->srcipaddr)) ||
(UIP_ICMP_BUF->icode != 0)) {
goto badpkt;
}
#endif /*UIP_CONF_IPV6_CHECKS*/
/*
* process the fields of the constant part
* As per RFC 4861, for reachable time and retrans timer, a value of 0 means
* unspecified by this router.
* If router lifetime is 0, the router SHOULD NOT be placed in default
* router list
*/
if(UIP_ND6_RA_BUF->cur_ttl != 0) {
uip_netif_physical_if.cur_hop_limit = UIP_ND6_RA_BUF->cur_ttl;
PRINTF("uip_netif_physical_if.cur_hop_limit %d\n", uip_netif_physical_if.cur_hop_limit);
}
/*
* As per RFC4861 section 6.3.4, if reachable time field is non zero
* and defers from the current base reachable time for the interface,
* set base reachable time to the new value and recompute the reachable time
* as a random value distributed between 0.5 and 1.5 x Base reachable time
*/
if(UIP_ND6_RA_BUF->reachable_time != 0) {
if(uip_netif_physical_if.base_reachable_time != ntohl(UIP_ND6_RA_BUF->reachable_time)) {
uip_netif_physical_if.base_reachable_time = ntohl(UIP_ND6_RA_BUF->reachable_time);
uip_netif_physical_if.reachable_time = uip_netif_compute_reachable_time();
}
}
if(UIP_ND6_RA_BUF->retrans_timer !=0) {
uip_netif_physical_if.retrans_timer = ntohl(UIP_ND6_RA_BUF->retrans_timer);
}
/*
* Note: in our implementation, an entry in the default router list contains
* does not contain the IP address of the router. The entry has a pointer
* to the corresponding neighbor cache entry, where the IP address is stored
*/
/* Options reading: possible options are MTU, SLLAO, prefix */
nd6_opt_llao = NULL;
nd6_opt_mtu = NULL;
for(i = 0; i < UIP_CONF_ND6_MAX_PREFIXES; i++) {
nd6_opt_prefix_info[i] = NULL;
}
nd6_opt_offset = UIP_ND6_RA_LEN;
while(uip_l3_icmp_hdr_len + nd6_opt_offset < uip_len) {
if(UIP_ND6_OPT_HDR_BUF->len == 0) {
goto badpkt;
}
switch(UIP_ND6_OPT_HDR_BUF->type) {
case UIP_ND6_OPT_SLLAO:
nd6_opt_llao = (struct uip_nd6_opt_llao *)UIP_ND6_OPT_HDR_BUF;
break;
case UIP_ND6_OPT_MTU:
nd6_opt_mtu = (struct uip_nd6_opt_mtu *)UIP_ND6_OPT_HDR_BUF;
break;
case UIP_ND6_OPT_PREFIX_INFO:
i = 0;
while((i < UIP_CONF_ND6_MAX_PREFIXES) && (nd6_opt_prefix_info[i] != NULL)) {
i++;
}
if(i < UIP_CONF_ND6_MAX_PREFIXES) {
nd6_opt_prefix_info[i] = (struct uip_nd6_opt_prefix_info *)UIP_ND6_OPT_HDR_BUF;
}
break;
default:
UIP_LOG("ND option not supported in RA");
break;
}
nd6_opt_offset += (UIP_ND6_OPT_HDR_BUF->len << 3);
}
/* Process the MTU option if any */
if(nd6_opt_mtu != NULL) {
/* just set the link MTU to the value specified. Note that this option */
PRINTF("Processing MTU option in RA\n");
uip_netif_physical_if.link_mtu = ntohl(nd6_opt_mtu->mtu);
}
/* Prefix information options processing */
i = 0;
while((i < UIP_CONF_ND6_MAX_PREFIXES) && (nd6_opt_prefix_info[i] != NULL)) {
PRINTF("Processing PREFIX option in RA\n");
if((ntohl((nd6_opt_prefix_info[i])->validlt) >=
ntohl((nd6_opt_prefix_info[i])->preferredlt)) &&
(!uip_is_addr_link_local(&nd6_opt_prefix_info[i]->prefix))) {
/* on-link flag related processing*/
if(((nd6_opt_prefix_info[i])->flagsreserved1 & 0x80) == 0x80) {
prefix = uip_nd6_prefix_lookup(&(nd6_opt_prefix_info[i])->prefix);
if(prefix == NULL) {
if((nd6_opt_prefix_info[i])->validlt != 0){
if((nd6_opt_prefix_info[i])->validlt != UIP_ND6_INFINITE_LIFETIME){
prefix = uip_nd6_prefix_add(&(nd6_opt_prefix_info[i])->prefix,
(nd6_opt_prefix_info[i])->preflen,
ntohl((nd6_opt_prefix_info[i])->validlt)*CLOCK_SECOND);
} else {
prefix = uip_nd6_prefix_add(&(nd6_opt_prefix_info[i])->prefix,
(nd6_opt_prefix_info[i])->preflen,
0);
}
}
} else {
/* we update or remove an existing prefix */
switch((nd6_opt_prefix_info[i])->validlt) {
case 0:
uip_nd6_prefix_rm(prefix);
break;
case UIP_ND6_INFINITE_LIFETIME:
prefix->is_infinite = 1;
break;
default:
timer_set(&prefix->vlifetime, ntohl((nd6_opt_prefix_info[i])->validlt)*CLOCK_SECOND);
/*in case the prefix lifetime was previously infinite */
prefix->is_infinite = 0;
break;
}
}
}
/* End of on-link flag related processing */
/* autonomous flag related processing*/
if((((nd6_opt_prefix_info[i])->flagsreserved1 & 0x40) == 0x40) &&
((nd6_opt_prefix_info[i])->validlt != 0)){
ifaddr = uip_netif_addr_lookup(&(nd6_opt_prefix_info[i])->prefix,
(nd6_opt_prefix_info[i])->preflen,
AUTOCONF);
if(ifaddr != NULL) {
if((nd6_opt_prefix_info[i])->validlt != UIP_ND6_INFINITE_LIFETIME) {
/* The processing below is defined in RFC4862 section 5.5.3 e */
if((ntohl((nd6_opt_prefix_info[i])->validlt) > 2 * 60 * 60) ||
(ntohl((nd6_opt_prefix_info[i])->validlt) * CLOCK_SECOND > timer_remaining(&ifaddr->vlifetime))) {
PRINTF("Updating timer of address");
PRINT6ADDR(&ifaddr->ipaddr);
PRINTF("new value %d\n", ntohl((nd6_opt_prefix_info[i])->validlt));
timer_set(&ifaddr->vlifetime, ntohl((nd6_opt_prefix_info[i])->validlt) * CLOCK_SECOND);
} else {
/** \TODO below overflows on raven, i.e. when times are on
16 bits */
timer_set(&ifaddr->vlifetime, 2 * 60 * 60 * CLOCK_SECOND);
PRINTF("Updating timer of address ");
PRINT6ADDR(&ifaddr->ipaddr);
PRINTF("new value %d\n", 2 * 60 * 60);
}
/*in case the address lifetime was previously infinite */
ifaddr->is_infinite = 0;
} else {
ifaddr->is_infinite = 1;
}
} else {
/* Autoconfigure an address*/
if((nd6_opt_prefix_info[i])->validlt != UIP_ND6_INFINITE_LIFETIME) {
/* Add an address with FINITE lifetime */
uip_netif_addr_add(&(nd6_opt_prefix_info[i])->prefix,
(nd6_opt_prefix_info[i])->preflen,
ntohl((nd6_opt_prefix_info[i])->validlt)*CLOCK_SECOND,
AUTOCONF);
} else {
/* Add an address with INFINITE lifetime */
uip_netif_addr_add(&(nd6_opt_prefix_info[i])->prefix,
(nd6_opt_prefix_info[i])->preflen,
0,
AUTOCONF);
}
}
}
/* End of autonomous flag related processing */
}
i++;
}
/* End of Prefix Information Option processing*/
/* SLLAO option processing */
neighbor = uip_nd6_nbrcache_lookup(&UIP_IP_BUF->srcipaddr);
if(nd6_opt_llao != NULL) {
PRINTF("Processing SLLAO option in RA\n");
if(neighbor == NULL) {
neighbor = uip_nd6_nbrcache_add(&UIP_IP_BUF->srcipaddr,
&nd6_opt_llao->addr, 1, STALE);
} else {
if(neighbor->state == INCOMPLETE) {
neighbor->state = STALE;
}
/* check if there was an address change. it applies to the case
* where if the router ND entry was in state NO_STATE */
if(memcmp(&nd6_opt_llao->addr, &neighbor->lladdr, UIP_LLADDR_LEN) != 0) {
memcpy(&neighbor->lladdr, &nd6_opt_llao->addr, UIP_LLADDR_LEN);
neighbor->state = STALE;
}
/*in case the neighbor was not a router before */
neighbor->isrouter = 1;
}
}
/*
* As per RFC4861, section 6.3.4, and considering that in our
* implementation, a router entry contains a pointer to an existing
* neighbor entry:
* if router lifetime != 0, if entry present in def router list
* (and therefore neighbor cache), update timer, and update neighbor entry
* if needed
* else add entry to both router list (and neighbor cache if absent)
* else delete entry in router list if it exists
*/
if(UIP_ND6_RA_BUF->router_lifetime != 0) {
/* Add router or update timer, we need a neighbor entry for this */
if(neighbor == NULL) {
neighbor = uip_nd6_nbrcache_add(&UIP_IP_BUF->srcipaddr, NULL, 1, NO_STATE);
} else {
/* in case the neighbor was not a router before */
neighbor->isrouter = 1;
}
if((router = uip_nd6_defrouter_lookup(neighbor)) == NULL) {
uip_nd6_defrouter_add(neighbor, ntohs(UIP_ND6_RA_BUF->router_lifetime)*CLOCK_SECOND);
} else {
timer_set(&(router->lifetime), ntohs(UIP_ND6_RA_BUF->router_lifetime)*CLOCK_SECOND);
}
} else {
/* delete router entry*/
if(neighbor != NULL) {
router = uip_nd6_defrouter_lookup(neighbor);
if(router != NULL) {
uip_nd6_defrouter_rm(router);
}
}
}
#if UIP_CONF_IPV6_QUEUE_PKT
/*
* If the neighbor just became reachable (e.g. it was in INCOMPLETE state
* and we got a SLLAO), check if we had buffered a pkt for it
*/
if((neighbor != NULL) && (neighbor->queue_buf_len != 0)) {
uip_len = neighbor->queue_buf_len;
memcpy(UIP_IP_BUF, neighbor->queue_buf, uip_len);
neighbor->queue_buf_len = 0;
return;
}
#endif /*UIP_CONF_IPV6_QUEUE_PKT*/
uip_len = 0;
return;
badpkt:
UIP_STAT(++uip_stat.nd6.drop);
UIP_LOG("RA received is bad");
uip_len = 0;
return;
}
/** @} */

471
core/net/uip-nd6.c Normal file
View File

@ -0,0 +1,471 @@
/**
* \addtogroup uip6
* @{
*/
/**
* \file
* Neighbor discovery (RFC 4861)
* \author Mathilde Durvy <mdurvy@cisco.com>
* \author Julien Abeille <jabeille@cisco.com>
*/
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include "net/uip-nd6.h"
#include "net/uip-netif.h"
#include "lib/random.h"
#include <string.h>
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",lladdr->addr[0], lladdr->addr[1], lladdr->addr[2], lladdr->addr[3],lladdr->addr[4], lladdr->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(addr)
#endif
#if UIP_LOGGING
#include <stdio.h>
void uip_log(char *msg);
#define UIP_LOG(m) uip_log(m)
#else
#define UIP_LOG(m)
#endif /* UIP_LOGGING == 1 */
/** Remove a neighbor cache entry */
#define uip_nd6_nbrcache_rm(neighbor) do { \
PRINTF("Removing neighbor with ip addr"); \
PRINT6ADDR(&neighbor->ipaddr); \
PRINTF("\n"); \
neighbor->used = 0; \
} while(0)
/**
* \brief Timer for maintenance of neighbor cache, prefix list, and default
* router lists
*/
struct etimer uip_nd6_timer_periodic;
/** \brief Neighor cache */
static struct uip_nd6_neighbor uip_nd6_nbrcache_list[UIP_CONF_ND6_MAX_NEIGHBORS];
/** \brief Default router list */
static struct uip_nd6_defrouter uip_nd6_defrouter_list[UIP_CONF_ND6_MAX_DEFROUTERS];
/** \brief Prefix list */
static struct uip_nd6_prefix uip_nd6_prefix_list[UIP_CONF_ND6_MAX_PREFIXES];
/** \name Temp variables*/
/** @{*/
static u8_t i;
static struct uip_nd6_prefix *prefix;
static struct uip_nd6_neighbor *neighbor;
static struct uip_nd6_defrouter *router;
/** @}*/
void
uip_nd6_init(void)
{
/* INITIALIZE NEIGHBOR DISCOVERY*/
for(i = 0; i < UIP_CONF_ND6_MAX_NEIGHBORS; i ++) {
uip_nd6_nbrcache_list[i].used = 0;
}
for(i = 0; i < UIP_CONF_ND6_MAX_DEFROUTERS; i ++) {
uip_nd6_defrouter_list[i].used = 0;
}
for(i = 0; i < UIP_CONF_ND6_MAX_PREFIXES; i ++) {
uip_nd6_prefix_list[i].used = 0;
}
/* create link local prefix */
uip_ip6addr(&(uip_nd6_prefix_list[0].ipaddr), 0xfe80, 0, 0, 0, 0, 0, 0, 0);
uip_nd6_prefix_list[0].length = UIP_DEFAULT_PREFIX_LEN;
uip_nd6_prefix_list[0].used = 1;
uip_nd6_prefix_list[0].is_infinite = 1;
/* we check the ND structures every 100ms */
etimer_set(&uip_nd6_timer_periodic, 0.1 * CLOCK_SECOND);
}
struct uip_nd6_neighbor *
uip_nd6_nbrcache_lookup(uip_ipaddr_t *ipaddr)
{
neighbor = NULL;
for(i = 0; i < UIP_CONF_ND6_MAX_NEIGHBORS; i ++) {
if(uip_nd6_nbrcache_list[i].used == 0) {
continue;
}
if(uip_ipaddr_cmp(&uip_nd6_nbrcache_list[i].ipaddr, ipaddr)) {
neighbor = &uip_nd6_nbrcache_list[i];
break;
}
}
return neighbor;
}
struct uip_nd6_neighbor *
uip_nd6_nbrcache_add(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr,
u8_t isrouter, uip_neighbor_state state)
{
neighbor = NULL;
for(i = 0; i < UIP_CONF_ND6_MAX_NEIGHBORS; ++i) {
if(uip_nd6_nbrcache_list[i].used == 0) {
break;
}
}
if(i == UIP_CONF_ND6_MAX_NEIGHBORS){
i = (u8_t)(random_rand()%UIP_CONF_ND6_MAX_NEIGHBORS);
UIP_LOG("CACHE FULL");
}
neighbor = &(uip_nd6_nbrcache_list[i]);
uip_ipaddr_copy(&(neighbor->ipaddr), ipaddr);
if(lladdr != NULL){
memcpy(&(neighbor->lladdr), lladdr, UIP_LLADDR_LEN);
} else {
memset(&(neighbor->lladdr), 0, UIP_LLADDR_LEN);
}
PRINTF("Adding neighbor with ip addr");
PRINT6ADDR(ipaddr);
PRINTF("link addr");
PRINTLLADDR((&(neighbor->lladdr)));
PRINTF("state %d\n", state);
neighbor->isrouter = isrouter;
neighbor->state = state;
/* timers are set separately, for now we put them in expired state */
timer_set(&(neighbor->reachable),0);
timer_set(&(neighbor->last_send),0);
neighbor->count_send = 0;
neighbor->used = 1;
return neighbor;
}
struct uip_nd6_defrouter *
uip_nd6_defrouter_lookup(struct uip_nd6_neighbor *neighbor)
{
router = NULL;
for(i = 0; i < UIP_CONF_ND6_MAX_DEFROUTERS; i ++) {
if(uip_nd6_defrouter_list[i].used == 0) {
continue;
}
if(uip_nd6_defrouter_list[i].nb == neighbor) {
router = &uip_nd6_defrouter_list[i];
break;
}
}
return router;
}
struct uip_nd6_defrouter *
uip_nd6_choose_defrouter(void)
{
router = NULL;
for(i = 0; i < UIP_CONF_ND6_MAX_DEFROUTERS; i ++) {
if(uip_nd6_defrouter_list[i].used == 0) {
continue;
}
if(uip_nd6_defrouter_list[i].nb == NULL){
continue;
}
if(uip_nd6_defrouter_list[i].nb->state == INCOMPLETE ||
uip_nd6_defrouter_list[i].nb->state == NO_STATE){
if(router == NULL)
router = &(uip_nd6_defrouter_list[i]);
} else {
router = &(uip_nd6_defrouter_list[i]);
break;
}
}
return router;
}
void
uip_nd6_defrouter_rm(struct uip_nd6_defrouter *router)
{
PRINTF("Removing defrouter with ip addr");
PRINT6ADDR(&((router->nb)->ipaddr));
PRINTF("\n");
router->used = 0;
if(((router->nb)->state) == NO_STATE){
/* Also remove the corresponding neighbor cache entry */
uip_nd6_nbrcache_rm(router->nb);
}
}
struct uip_nd6_defrouter *
uip_nd6_defrouter_add(struct uip_nd6_neighbor *neighbor, clock_time_t interval)
{
router = NULL;
for(i = 0; i < UIP_CONF_ND6_MAX_DEFROUTERS; ++i) {
if(uip_nd6_defrouter_list[i].used == 0) {
break;
}
}
if(i == UIP_CONF_ND6_MAX_DEFROUTERS){
UIP_LOG("Defrouter list full");
i = (u8_t)(random_rand()%UIP_CONF_ND6_MAX_DEFROUTERS);
}
router = &(uip_nd6_defrouter_list[i]);
router->nb = neighbor;
PRINTF("Adding defrouter with ip addr");
PRINT6ADDR(&neighbor->ipaddr);
PRINTF("\n");
timer_set(&(router->lifetime),interval);
uip_nd6_defrouter_list[i].used = 1;
return router;
}
u8_t
uip_nd6_is_addr_onlink(uip_ipaddr_t *ipaddr)
{
for(i = 0; i < UIP_CONF_ND6_MAX_PREFIXES; i ++) {
if(uip_nd6_prefix_list[i].used == 0){
continue;
}
if(uip_ipaddr_prefixcmp(&uip_nd6_prefix_list[i].ipaddr,
ipaddr, uip_nd6_prefix_list[i].length)){
return 1;
}
}
return 0;
}
struct uip_nd6_prefix *
uip_nd6_prefix_lookup(uip_ipaddr_t *ipaddr)
{
prefix = NULL;
for (i = 0; i < UIP_CONF_ND6_MAX_PREFIXES; i ++) {
if (uip_nd6_prefix_list[i].used == 0){
continue;
}
if (uip_ipaddr_cmp(&uip_nd6_prefix_list[i].ipaddr, ipaddr)) {
prefix = &uip_nd6_prefix_list[i];
break;
}
}
return prefix;
}
struct uip_nd6_prefix *
uip_nd6_prefix_add(uip_ipaddr_t *ipaddr, u8_t length, clock_time_t interval){
prefix = NULL;
/*
* we start at 1: we do not want to overwrite the link local
* prefix
*/
for(i = 1; i < UIP_CONF_ND6_MAX_PREFIXES; ++i) {
if(uip_nd6_prefix_list[i].used == 0) {
break;
}
}
if(i == UIP_CONF_ND6_MAX_PREFIXES){
UIP_LOG("Prefix list full");
/*
* we do not want it to pick the first entry, which
* is the link local prefix
*/
i = (u8_t)(random_rand()%(UIP_CONF_ND6_MAX_PREFIXES -1) + 1);
PRINTF("Prefix list full, picking index %x\n\n", i);
}
prefix = &(uip_nd6_prefix_list[i]);
uip_ipaddr_copy(&(prefix->ipaddr), ipaddr);
prefix->length = length;
PRINTF("Adding prefix ");
PRINT6ADDR(&prefix->ipaddr);
PRINTF("length %d, vlifetime * CLOCK_SECOND %d\n", length, (u16_t)interval);
if(interval != 0){
timer_set(&(prefix->vlifetime),interval);
prefix->is_infinite = 0;
} else {
prefix->is_infinite = 1;
}
uip_nd6_prefix_list[i].used = 1;
return prefix;
}
void uip_nd6_prefix_rm(struct uip_nd6_prefix *prefix) {
PRINTF("Removing prefix ");
PRINT6ADDR(&prefix->ipaddr);
PRINTF("length %d\n", prefix->length);
prefix->used = 0;
}
void
uip_nd6_periodic(void)
{
etimer_reset(&uip_nd6_timer_periodic);
/*PERIODIC PROCESSING FOR DEFAULT ROUTER LIST*/
router = NULL;
for(i = 0; i < UIP_CONF_ND6_MAX_DEFROUTERS; i ++) {
if(uip_nd6_defrouter_list[i].used == 1) {
router = &(uip_nd6_defrouter_list[i]);
if (timer_expired(&(router->lifetime))) {
uip_nd6_defrouter_rm(router);
}
}
}
/*PERIODIC PROCESSING FOR NEIGHBOR CACHE*/
neighbor = NULL;
for(i = 0; i < UIP_CONF_ND6_MAX_NEIGHBORS; i ++) {
if(uip_nd6_nbrcache_list[i].used == 1) {
neighbor = &(uip_nd6_nbrcache_list[i]);
switch (neighbor->state) {
case INCOMPLETE:
if(neighbor->count_send >= UIP_ND6_MAX_MULTICAST_SOLICIT) {
uip_nd6_nbrcache_list[i].used = 0;
}
else if(timer_expired(&(neighbor->last_send))) {
PRINTF("INCOMPLETE: NS %d\n",neighbor->count_send+1);
uip_nd6_io_ns_output(NULL, NULL, &neighbor->ipaddr);
timer_set(&(neighbor->last_send),
uip_netif_physical_if.retrans_timer/1000*CLOCK_SECOND);
neighbor->count_send++;
}
break;
case REACHABLE:
if(timer_expired(&(neighbor->reachable))) {
PRINTF("REACHABLE: moving to STALE (");
PRINT6ADDR(&neighbor->ipaddr);
PRINTF(")\n");
neighbor->state = STALE;
neighbor->count_send = 0;
}
break;
case DELAY:
if(timer_expired(&(neighbor->reachable))) {
neighbor->state = PROBE;
neighbor->count_send = 0;
PRINTF("DELAY: moving to PROBE + NS %d\n", neighbor->count_send+1);
uip_nd6_io_ns_output(NULL, &neighbor->ipaddr, &neighbor->ipaddr);
timer_set(&(neighbor->last_send),
uip_netif_physical_if.retrans_timer/1000*CLOCK_SECOND);
neighbor->count_send++;
}
break;
case PROBE:
if(neighbor->count_send >= UIP_ND6_MAX_UNICAST_SOLICIT) {
PRINTF("PROBE END \n");
neighbor->count_send = 0;
/* remove any corresponding default router (if any)*/
router = uip_nd6_defrouter_lookup(neighbor);
if(router != NULL)
uip_nd6_defrouter_rm(router);
/* remove neighbor cache entry */
uip_nd6_nbrcache_rm(neighbor);
continue;
}
if(timer_expired(&(neighbor->last_send))){
PRINTF("PROBE: NS %d\n",neighbor->count_send+1);
uip_nd6_io_ns_output(NULL, &neighbor->ipaddr, &neighbor->ipaddr);
timer_set(&(neighbor->last_send),
uip_netif_physical_if.retrans_timer/1000*CLOCK_SECOND);
neighbor->count_send++;
}
break;
default:
break;
}
}
}
/*PERIODIC PROCESSING FOR PREFIX LIST*/
prefix = NULL;
/*
* we start with i=1 because the first prefix is the link-local prefix
* and therefore has INFINITE an lifetime
*/
for(i = 1; i < UIP_CONF_ND6_MAX_PREFIXES; i ++) {
if(uip_nd6_prefix_list[i].used == 1) {
prefix = &(uip_nd6_prefix_list[i]);
if((prefix->is_infinite == 0) &&
(timer_expired(&(prefix->vlifetime)))) {
/* remove prefix */
uip_nd6_prefix_rm(prefix);
}
}
}
}
/** @} */

688
core/net/uip-nd6.h Normal file
View File

@ -0,0 +1,688 @@
/**
* \addtogroup uip6
* @{
*/
/**
* \file
* Neighbor discovery (RFC 4861)
* \author Julien Abeille <jabeille@cisco.com>
* \author Mathilde Durvy <mdurvy@cisco.com>
*/
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
*/
#ifndef __UIP_ND6_H__
#define __UIP_ND6_H__
#include "net/uip.h"
/**
* \name General
* @{
*/
/** \brief HOP LIMIT to be used when sending ND messages (255) */
#define UIP_ND6_HOP_LIMIT 255
/** \brief INFINITE lifetime */
#define UIP_ND6_INFINITE_LIFETIME 0xFFFFFFFF
/** @} */
/** \name Configuration options */
/** @{ */
#ifndef UIP_CONF_ND6_MAX_NEIGHBORS
/** \brief max number of entries in the neighbor cache */
#define UIP_CONF_ND6_MAX_NEIGHBORS 4
#endif /*UIP_CONF_ND6_MAX_NEIGHBORS*/
#ifndef UIP_CONF_ND6_MAX_DEFROUTERS
/** \brief max number of entries in the default router cache */
#define UIP_CONF_ND6_MAX_DEFROUTERS 2
#endif /*UIP_CONF_ND6_MAX_DEFROUTERS*/
#ifndef UIP_CONF_ND6_MAX_PREFIXES
/** \brief max number of entries in the prefix list */
#define UIP_CONF_ND6_MAX_PREFIXES 2
#endif /*UIP_CONF_ND6_MAX_PREFIXES*/
/** @} */
/** \name RFC 4861 Host constant */
/** @{ */
#define UIP_ND6_MAX_RTR_SOLICITATION_DELAY 1
#define UIP_ND6_RTR_SOLICITATION_INTERVAL 4
#define UIP_ND6_MAX_RTR_SOLICITATIONS 3
/** @} */
/** \name RFC 4861 Node constant */
#define UIP_ND6_MAX_MULTICAST_SOLICIT 3
#define UIP_ND6_MAX_UNICAST_SOLICIT 3
#define UIP_ND6_REACHABLE_TIME 30000
#define UIP_ND6_RETRANS_TIMER 1000
#define UIP_ND6_DELAY_FIRST_PROBE_TIME 5
#define UIP_ND6_MIN_RANDOM_FACTOR 0.5
#define UIP_ND6_MAX_RANDOM_FACTOR 1.5
/** @} */
/** \name ND6 option types */
/** @{ */
#define UIP_ND6_OPT_SLLAO 1
#define UIP_ND6_OPT_TLLAO 2
#define UIP_ND6_OPT_PREFIX_INFO 3
#define UIP_ND6_OPT_REDIRECTED_HDR 4
#define UIP_ND6_OPT_MTU 5
/** @} */
/** \name ND6 message length (excluding options) */
/** @{ */
#define UIP_ND6_NA_LEN 20
#define UIP_ND6_NS_LEN 20
#define UIP_ND6_RA_LEN 12
#define UIP_ND6_RS_LEN 4
/** @} */
/** \name ND6 option length in bytes */
/** @{ */
#define UIP_ND6_OPT_HDR_LEN 2
#define UIP_ND6_OPT_PREFIX_INFO_LEN 32
#define UIP_ND6_OPT_MTU_LEN 8
/* Length of TLLAO and SLLAO options, it is L2 dependant */
#if UIP_CONF_LL_802154
/* If the interface is 802.15.4. For now we use only long addresses */
#define UIP_ND6_OPT_SHORT_LLAO_LEN 8
#define UIP_ND6_OPT_LONG_LLAO_LEN 16
/** \brief length of a ND6 LLAO option for 802.15.4 */
#define UIP_ND6_OPT_LLAO_LEN UIP_ND6_OPT_LONG_LLAO_LEN
#else /*UIP_CONF_LL_802154*/
#if UIP_CONF_LL_80211
/* If the interface is 802.11 */
/** \brief length of a ND6 LLAO option for 802.11 */
#define UIP_ND6_OPT_LLAO_LEN 8
#else /*UIP_CONF_LL_80211*/
/** \brief length of a ND6 LLAO option for default L2 type (e.g. Ethernet) */
#define UIP_ND6_OPT_LLAO_LEN 8
#endif /*UIP_CONF_LL_80211*/
#endif /*UIP_CONF_LL_802154*/
/** @} */
/** \name Neighbor Advertisement flags masks */
/** @{ */
#define UIP_ND6_NA_FLAG_ROUTER 0x80
#define UIP_ND6_NA_FLAG_SOLICITED 0x40
#define UIP_ND6_NA_FLAG_OVERRIDE 0x20
/** @} */
/**
* \brief Possible states for the neighbor cache entries
*
* NO_STATE is for implementation purposes: a router entry contains a pointer
* to a neighbor entry, which holds its ip address. If we do not know the LL
* address of the router, we do not have to create a neighbor entry as per
* RFC 4861. However, we still need to have the IP of the router stored in a
* neighbor entry, hence we create an entry in the NO_STATE state
*/
typedef enum {
INCOMPLETE = 0,
REACHABLE = 1,
STALE = 2,
DELAY = 3,
PROBE = 4,
NO_STATE = 5
} uip_neighbor_state;
/**
* \name ND structures
* @{
*/
/** \brief An entry in the neighbor cache */
struct uip_nd6_neighbor{
uip_ipaddr_t ipaddr;
uip_lladdr_t lladdr;
u8_t isrouter;
uip_neighbor_state state;
struct timer reachable;
struct timer last_send; /**< last time a ND message was sent */
u8_t count_send; /**< how many ND message were already sent */
u8_t used; /**< brief is this neighbor currently used */
#if UIP_CONF_IPV6_QUEUE_PKT
u8_t queue_buf[UIP_BUFSIZE - UIP_LLH_LEN];
/**< buffer to hold one packet during address resolution */
u8_t queue_buf_len;
/**< length of the pkt in buffer, used as "boolean" as well*/
#endif /*UIP_CONF_QUEUE_PKT*/
};
/** \brief An entry in the default router list */
struct uip_nd6_defrouter {
struct uip_nd6_neighbor *nb;
struct timer lifetime;
/**< the lifetime contained in RA corresponds to the interval field
* of the timer
*/
u8_t used; /**< Is this default router entry currently used */
};
/** \brief A prefix list entry */
struct uip_nd6_prefix {
uip_ipaddr_t ipaddr;
u8_t length;
/**< we do not use preferred lifetime, which is always smaller than
* valid lifetime (for addr, preferred->deprecated)
*/
struct timer vlifetime;
u8_t is_infinite; /**< Is the prefix lifetime INFINITE */
u8_t used; /**< Is this prefix entry currently used */
};
/** @} */
extern struct etimer uip_nd6_timer_periodic;
/**
* \note
* We do not use a destination cache, do next-hop determination each time
* a packet needs to be sent. (info such as rtt, path mtu could be stored
* in uip_conn)
*
*/
/**
* \name ND message structures
* @{
*/
/**
* \brief A neighbor solicitation constant part
*
* Possible option is: SLLAO
*/
struct uip_nd6_ns {
u32_t reserved;
uip_ipaddr_t tgtipaddr;
};
/**
* \brief A neighbor advertisement constant part.
*
* Possible option is: TLLAO
*/
struct uip_nd6_na {
u8_t flagsreserved;
u8_t reserved[3];
uip_ipaddr_t tgtipaddr;
};
/**
* \brief A router solicitation constant part
*
* Possible option is: SLLAO
*/
struct uip_nd6_rs {
u32_t reserved;
};
/**
* \brief A router advertisement constant part
*
* Possible options are: SLLAO, MTU, Prefix Information
*/
struct uip_nd6_ra {
u8_t cur_ttl;
u8_t flags_reserved;
u16_t router_lifetime;
u32_t reachable_time;
u32_t retrans_timer;
};
/**
* \brief A redirect message constant part
*
* Possible options are: TLLAO, redirected header
*/
struct uip_nd6_redirect {
u32_t reserved;
uip_ipaddr_t tgtipaddress;
uip_ipaddr_t destipaddress;
};
/** @} */
/**
* \name ND Option structures
* @{
*/
/** \brief ND option header */
struct uip_nd6_opt_hdr {
u8_t type;
u8_t len;
};
/** \brief ND option prefix information */
struct uip_nd6_opt_prefix_info {
u8_t type;
u8_t len;
u8_t preflen;
u8_t flagsreserved1;
u32_t validlt;
u32_t preferredlt;
u32_t reserved2;
uip_ipaddr_t prefix;
};
/** \brief ND option MTU */
struct uip_nd6_opt_mtu {
u8_t type;
u8_t len;
u16_t reserved;
u32_t mtu;
};
/** \brief ND option: both TLLAO and SLLAO */
struct uip_nd6_opt_llao {
u8_t type;
u8_t len;
uip_lladdr_t addr;
};
/** \struct Redirected header option */
struct uip_nd6_opt_redirected_hdr {
u8_t type;
u8_t len;
u8_t reserved[6];
};
/** @} */
/**
* \name ND Neighbor Cache, Router List and Prefix List handling functions
* @{
*/
/**
* \brief Initialize Neighbor Discovery structures
*/
void uip_nd6_init(void);
/**
* \brief Periodic processing of Neighbor Discovery Structures
*/
void uip_nd6_periodic(void);
/**
* \brief Look for a neighbor cache entry corresponding to a specific IP
* address
* \param ipaddr the specific IP address
* \return the corresponding neighbor cache entry
*/
struct uip_nd6_neighbor *uip_nd6_nbrcache_lookup(uip_ipaddr_t *ipaddr);
/**
* \brief Add a neighbor cache entry
* \param ipaddr the IP address of the entry
* \param lladdr the layer 2 address of the entry
* \param isrouter true is the entry is a router
* \param state the state of the entry
* \return the new neighbor or updated cache entry
*/
struct uip_nd6_neighbor * uip_nd6_nbrcache_add(uip_ipaddr_t *ipaddr,
uip_lladdr_t *lladdr,
u8_t isrouter,
uip_neighbor_state state);
/**
* \brief Returns a default router
*/
struct uip_nd6_defrouter * uip_nd6_choose_defrouter(void);
/**
* \brief Find a default router corresponding to a given neighbor cache entry
* \param neighbor the neighbor cache entry
* \return the corresponding router if any
*/
struct uip_nd6_defrouter *
uip_nd6_defrouter_lookup(struct uip_nd6_neighbor *neighbor);
/**
* \brief Remove a default router
* \param router to be removed
*/
void uip_nd6_defrouter_rm(struct uip_nd6_defrouter *router);
/**
* \brief Add a default router
* \param neighbor the corresponding neighbor cache entry
* \param interval the lifetime of the router
* \return the new or updated defrouter entry
*/
struct uip_nd6_defrouter *
uip_nd6_defrouter_add(struct uip_nd6_neighbor *neighbor, clock_time_t interval);
/**
* \brief Check if an IP address in on-link by looking at prefix list
* \param ipaddr an IP address
* \return true if on-link
*/
u8_t uip_nd6_is_addr_onlink(uip_ipaddr_t *ipaddr);
/**
* \brief Find a given prefix
* \param ipaddr an IP address
* \return the corresponding prefix if any
*/
struct uip_nd6_prefix *
uip_nd6_prefix_lookup(uip_ipaddr_t *ipaddr);
/**
* \brief Add a prefix
* \param ipaddr the IP address of the prefix
* \param length the length of the prefix
* \param interval the lifetime of the prefix
* \return the new or updated prefix entry
*/
struct uip_nd6_prefix *
uip_nd6_prefix_add(uip_ipaddr_t *ipaddr, u8_t length, clock_time_t interval);
/**
* \brief Remove a prefix from th eprefix list
* \param prefix pointer to the prefix to be removed
*/
void
uip_nd6_prefix_rm(struct uip_nd6_prefix *prefix);
/** @} */
/**
* \name ND Messages Processing and Generation
* @{
*/
/**
* \brief Process a neighbor solicitation
*
* The NS can be received in 3 cases (procedures):
* - sender is performing DAD (ip src = unspecified, no SLLAO option)
* - sender is performing NUD (ip dst = unicast)
* - sender is performing address resolution (ip dest = solicited node mcast
* address)
*
* We do:
* - if the tgt belongs to me, reply, otherwise ignore
* - if i was performing DAD for the same address, two cases:
* -- I already sent a NS, hence I win
* -- I did not send a NS yet, hence I lose
*
* If we need to send a NA in response (i.e. the NS was done for NUD, or
* address resolution, or DAD and there is a conflict), we do it in this
* function: set src, dst, tgt address in the three cases, then for all cases
* set the rest, including SLLAO
*
*/
void
uip_nd6_io_ns_input(void);
/**
* \brief Send a neighbor solicitation, send a Neighbor Advertisement
* \param src pointer to the src of the NS if known
* \param dest pointer to ip address to send the NS, for DAD or ADDR Resol,
* MUST be NULL, for NUD, must be correct unicast dest
* \param tgt pointer to ip address to fill the target address field, must
* not be NULL
*
* - RFC 4861, 7.2.2 :
* "If the source address of the packet prompting the solicitation is the
* same as one of the addresses assigned to the outgoing interface, that
* address SHOULD be placed in the IP Source Address of the outgoing
* solicitation. Otherwise, any one of the addresses assigned to the
* interface should be used."
* This is why we have a src ip address as argument. If NULL, we will do
* src address selection, otherwise we use the argument.
*
* - we check if it is a NS for Address resolution or NUD, if yes we include
* a SLLAO option, otherwise no.
*/
void
uip_nd6_io_ns_output(uip_ipaddr_t *src, uip_ipaddr_t *dest, uip_ipaddr_t *tgt);
/**
* \brief Process a Neighbor Advertisement
*
* we might have to send a pkt that had been buffered while address
* resolution was performed (if we support buffering, see UIP_CONF_QUEUE_PKT)
*
* As per RFC 4861, on link layer that have addresses, TLLAO options MUST be
* included when responding to multicast solicitations, SHOULD be included in
* response to unicast (here we assume it is for now)
*
* NA can be received after sending NS for DAD, Address resolution or NUD. Can
* be unsolicited as well.
* It can trigger update of the state of the neighbor in the neighbor cache,
* router in the router list.
* If the NS was for DAD, it means DAD failed
*
*/
void
uip_nd6_io_na_input(void);
/**
* \brief Send a Router Solicitation
*
* src is chosen through the uip_netif_select_src function. If src is
* unspecified (i.e. we do not have a preferred address yet), then we do not
* put a SLLAO option (MUST NOT in RFC 4861). Otherwise we do.
*
* RS message format,
* possible option is SLLAO, MUST NOT be included if source = unspecified
* SHOULD be included otherwise
*/
void uip_nd6_io_rs_output(void);
/**
*
* \brief process a Router Advertisement
*
* - Possible actions when receiving a RA: add router to router list,
* recalculate reachable time, update link hop limit, update retrans timer.
* - If MTU option: update MTU.
* - If SLLAO option: update entry in neighbor cache
* - If prefix option: start autoconf, add prefix to prefix list
*/
void
uip_nd6_io_ra_input(void);
/** @} */
void
uip_appserver_addr_get(uip_ipaddr_t *ipaddr);
/*--------------------------------------*/
/******* ANNEX - message formats ********/
/*--------------------------------------*/
/*
* RS format. possible option is SLLAO
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type | Code | Checksum |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Options ...
* +-+-+-+-+-+-+-+-+-+-+-+-
*
*
* RA format. possible options: prefix information, MTU, SLLAO
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type | Code | Checksum |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Cur Hop Limit |M|O| Reserved | Router Lifetime |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Reachable Time |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Retrans Timer |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Options ...
* +-+-+-+-+-+-+-+-+-+-+-+-
*
*
* NS format: options should be SLLAO
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type | Code | Checksum |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* + +
* | |
* + Target Address +
* | |
* + +
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Options ...
* +-+-+-+-+-+-+-+-+-+-+-+-
*
*
* NA message format. possible options is TLLAO
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type | Code | Checksum |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |R|S|O| Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* + +
* | |
* + Target Address +
* | |
* + +
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Options ...
* +-+-+-+-+-+-+-+-+-+-+-+-
*
*
* Redirect message format. Possible options are TLLAO and Redirected header
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type | Code | Checksum |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* + +
* | |
* + Target Address +
* | |
* + +
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* + +
* | |
* + Destination Address +
* | |
* + +
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Options ...
* +-+-+-+-+-+-+-+-+-+-+-+-
*
*
* SLLAO/TLLAO option:
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type | Length | Link-Layer Address ...
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
*
* Prefix information option
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type | Length | Prefix Length |L|A| Reserved1 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Valid Lifetime |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Preferred Lifetime |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Reserved2 |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* + +
* | |
* + Prefix +
* | |
* + +
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
*
* MTU option
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type | Length | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | MTU |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
*
* Redirected header option
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Type | Length | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* ~ IP header + data ~
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
*/
#endif /* __UIP_ND6_H__ */
/** @} */

445
core/net/uip-netif.c Normal file
View File

@ -0,0 +1,445 @@
/**
* \addtogroup uip6
* @{
*/
/**
* \file
* Network interface and stateless autoconfiguration (RFC 4862)
* \author Mathilde Durvy <mdurvy@cisco.com>
* \author Julien Abeille <jabeille@cisco.com>
*
*/
/*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*
*/
#include "net/uip-nd6.h"
#include "net/uip-netif.h"
#include "lib/random.h"
#include <string.h>
#include <stdlib.h>
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
#define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",lladdr->addr[0], lladdr->addr[1], lladdr->addr[2], lladdr->addr[3],lladdr->addr[4], lladdr->addr[5])
#else
#define PRINTF(...)
#define PRINT6ADDR(addr)
#define PRINTLLADDR(lladdr)
#endif
#if UIP_LOGGING
#include <stdio.h>
void uip_log(char *msg);
#define UIP_LOG(m) uip_log(m)
#else
#define UIP_LOG(m)
#endif /* UIP_LOGGING == 1 */
/*---------------------------------------------------------------------------*/
/** \brief The single physical interface */
struct uip_netif uip_netif_physical_if;
/** \brief DAD timer */
struct etimer uip_netif_timer_dad;
/** \brief The interface address on which dad is being performed */
static struct uip_netif_addr *dad_ifaddr;
/** \brief Number of ns already sent for DAD*/
static u8_t dad_ns;
/** \brief RS timer, to schedule RS sending */
struct etimer uip_netif_timer_rs;
/** \brief number of rs already sent */
static u8_t rs_count;
/** \brief index for loops */
static u8_t i;
/** \brief timer to check the address states */
struct etimer uip_netif_timer_periodic;
/** \brief remove an interface address, argument type is uip_netif_addr* */
#define uip_netif_addr_rm(addr) do { \
PRINTF("Removing address"); \
PRINT6ADDR(&addr->ipaddr); \
PRINTF("\n"); \
addr->state = NOT_USED; \
} while(0)
/*---------------------------------------------------------------------------*/
void
uip_netif_init(void)
{
/* INITIALIZE INTERFACE (default values for now) */
uip_netif_physical_if.link_mtu = UIP_LINK_MTU;
uip_netif_physical_if.cur_hop_limit = UIP_TTL;
uip_netif_physical_if.base_reachable_time = UIP_ND6_REACHABLE_TIME;
uip_netif_physical_if.reachable_time = uip_netif_compute_reachable_time();
uip_netif_physical_if.retrans_timer = UIP_ND6_RETRANS_TIMER;
uip_netif_physical_if.dup_addr_detect_transmit = 1;
/*
* STATELESS AUTOCONFIGURATION of the link local address. We set it to
* infinite (this will become really true once DAD succeeds)
*/
uip_ip6addr(&(uip_netif_physical_if.addresses[0].ipaddr),
0xfe80,0,0,0,0,0,0,0);
uip_netif_addr_autoconf_set(&(uip_netif_physical_if.addresses[0].ipaddr), &uip_lladdr);
uip_netif_physical_if.addresses[0].state = TENTATIVE;
uip_netif_physical_if.addresses[0].type = MANUAL;
uip_netif_physical_if.addresses[0].is_infinite = 1;
/* set all other addresses to NOT_USED initialy */
for(i = 1; i < UIP_CONF_NETIF_MAX_ADDRESSES; i ++)
uip_netif_physical_if.addresses[i].state = NOT_USED;
uip_ip6addr_u8(&(uip_netif_physical_if.solicited_node_mcastaddr),
0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0xff,
uip_lladdr.addr[UIP_LLADDR_LEN - 3],
uip_lladdr.addr[UIP_LLADDR_LEN - 2],
uip_lladdr.addr[UIP_LLADDR_LEN - 1]);
/* Start DAD */
uip_netif_sched_dad(&(uip_netif_physical_if.addresses[0]));
/* Find router (send rs to all-routers multicast group)) */
uip_netif_sched_send_rs();
/* Reset the timer */
etimer_set(&uip_netif_timer_periodic, CLOCK_SECOND);
}
/*---------------------------------------------------------------------------*/
void
uip_netif_periodic(void) {
for(i = 1; i < UIP_CONF_NETIF_MAX_ADDRESSES; i++) {
if((uip_netif_physical_if.addresses[i].state != NOT_USED) &&
(uip_netif_physical_if.addresses[i].is_infinite != 1) &&
(timer_expired(&uip_netif_physical_if.addresses[i].vlifetime))) {
uip_netif_addr_rm((&uip_netif_physical_if.addresses[i]));
}
}
etimer_reset(&uip_netif_timer_periodic);
return;
}
/*---------------------------------------------------------------------------*/
u32_t
uip_netif_compute_reachable_time(void)
{
return (u32_t)(uip_netif_physical_if.base_reachable_time * UIP_ND6_MIN_RANDOM_FACTOR) + ((u16_t)(random_rand() << 8) + (u16_t)random_rand()) % (u32_t)(uip_netif_physical_if.base_reachable_time *(UIP_ND6_MAX_RANDOM_FACTOR - UIP_ND6_MIN_RANDOM_FACTOR));
}
/*---------------------------------------------------------------------------*/
u8_t
uip_netif_is_addr_my_solicited(uip_ipaddr_t *ipaddr)
{
if(uip_ipaddr_cmp(ipaddr, &uip_netif_physical_if.solicited_node_mcastaddr))
return 1;
return 0;
}
/*---------------------------------------------------------------------------*/
struct uip_netif_addr *
uip_netif_addr_lookup(uip_ipaddr_t *ipaddr, u8_t length, uip_netif_type type) {
for(i = 0; i < UIP_CONF_NETIF_MAX_ADDRESSES; i ++) {
if((uip_netif_physical_if.addresses[i].state != NOT_USED) &&
(uip_netif_physical_if.addresses[i].type == type || type == 0) &&
(uip_ipaddr_prefixcmp(&(uip_netif_physical_if.addresses[i].ipaddr), ipaddr, length))) {
return &uip_netif_physical_if.addresses[i];
}
}
return NULL;
}
/*---------------------------------------------------------------------------*/
void
uip_netif_addr_add(uip_ipaddr_t *ipaddr, u8_t length, clock_time_t vlifetime, uip_netif_type type) {
/* check prefix has the right length if we are doing autoconf */
if((type == AUTOCONF) && (length != UIP_DEFAULT_PREFIX_LEN)) {
UIP_LOG("Error: UNSUPPORTED PREFIX LENGTH");
return;
}
/* check if addr does not already exist and find a free entry */
for(i = 0; i < UIP_CONF_NETIF_MAX_ADDRESSES; ++i) {
if(uip_netif_physical_if.addresses[i].state == NOT_USED){
/*
* Copying address
* If we are doing autoconf, ipaddr is a prefix, we copy the 128 bits
* of it, then overwrite the last 64 bits with the interface ID at
* next if statement.
* Otherwise ipaddr is an address, we just copy it
*/
uip_ipaddr_copy(&uip_netif_physical_if.addresses[i].ipaddr, ipaddr);
if(type == AUTOCONF) {
/* construct address from prefix and layer2 id */
uip_netif_addr_autoconf_set(&uip_netif_physical_if.addresses[i].ipaddr, &uip_lladdr);
}
/* setting state, type */
uip_netif_physical_if.addresses[i].state = TENTATIVE;
uip_netif_physical_if.addresses[i].type = type;
/* setting lifetime timer if lieftime is not infinite */
if(vlifetime != 0) {
timer_set(&(uip_netif_physical_if.addresses[i].vlifetime), vlifetime);
uip_netif_physical_if.addresses[i].is_infinite = 0;
} else {
uip_netif_physical_if.addresses[i].is_infinite = 1;
}
PRINTF("Created new address");
PRINT6ADDR(&uip_netif_physical_if.addresses[i].ipaddr);
PRINTF("for interface\n");
/* schedule DAD */
uip_netif_sched_dad(&uip_netif_physical_if.addresses[i]);
return;
}
}
/* If we did not find space, log */
UIP_LOG("ADDRESS LIST FULL");
return;
}
/*---------------------------------------------------------------------------*/
void
uip_netif_addr_autoconf_set(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr)
{
/* We consider only links with IEEE EUI-64 identifier or
IEEE 48-bit MAC addresses */
#if (UIP_LLADDR_LEN == 8)
memcpy(ipaddr->u8 + 8, lladdr, UIP_LLADDR_LEN);
ipaddr->u8[8] ^= 0x02;
#elif (UIP_LLADDR_LEN == 6)
memcpy(ipaddr->u8 + 8, lladdr, 3);
ipaddr->u8[11] = 0xff;
ipaddr->u8[12] = 0xfe;
memcpy(ipaddr->u8 + 13, lladdr + 3, 3);
ipaddr->u8[8] ^= 0x02;
#else
UIP_LOG("CAN NOT BUIL INTERFACE IDENTIFIER");
UIP_LOG("THE STACK IS GOING TO SHUT DOWN");
UIP_LOG("THE HOST WILL BE UNREACHABLE");
exit(-1);
#endif
}
/*---------------------------------------------------------------------------*/
u8_t
get_match_length(uip_ipaddr_t *src, uip_ipaddr_t *dst)
{
u8_t j, k, x_or;
u8_t len = 0;
for(j = 0; j < 16; j ++) {
if(src->u8[j] == dst->u8[j]) {
len += 8;
} else {
x_or = src->u8[j] ^ dst->u8[j];
for(k = 0; k < 8; k ++) {
if((x_or & 0x80) == 0){
len ++;
x_or <<= 1;
}
else {
break;
}
}
break;
}
}
return len;
}
/*---------------------------------------------------------------------------*/
void
uip_netif_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst)
{
u8_t best = 0; /* number of bit in common with best match*/
u8_t n = 0;
u8_t index = 0;
if(!uip_is_addr_link_local(dst) && !uip_is_addr_mcast(dst)) {
for(i = 1; i < UIP_CONF_NETIF_MAX_ADDRESSES; ++i) {
if(uip_netif_physical_if.addresses[i].state == PREFERRED){
n = get_match_length(dst, &(uip_netif_physical_if.addresses[i].ipaddr));
if(n >= best){
best = n;
index = i;
}
}
}
}
uip_ipaddr_copy(src, &(uip_netif_physical_if.addresses[index].ipaddr));
return;
}
/*---------------------------------------------------------------------------*/
void
uip_netif_sched_dad(struct uip_netif_addr *ifaddr)
{
if(ifaddr->state != TENTATIVE){
UIP_LOG("DAD called with non tentative address");
return;
}
/*
* check dad is not running
*/
if(dad_ifaddr != NULL){
UIP_LOG("DAD already running");
return;
}
/*
* Set the interface address that is going through DAD
*/
dad_ifaddr = ifaddr;
PRINTF("Scheduling DAD for ipaddr:");
PRINT6ADDR(&dad_ifaddr->ipaddr);
PRINTF("\n");
etimer_set(&uip_netif_timer_dad, random_rand()%(UIP_ND6_MAX_RTR_SOLICITATION_DELAY*CLOCK_SECOND));
}
/*---------------------------------------------------------------------------*/
void
uip_netif_dad(void)
{
/*
* check if dad is running
*/
if(dad_ifaddr == NULL){
PRINTF("uip_netif_dad: DAD not running\n");
return;
}
/*
* send dup_addr_detect_transmit NS for DAD
*/
if(dad_ns < uip_netif_physical_if.dup_addr_detect_transmit) {
uip_nd6_io_ns_output(NULL, NULL, &dad_ifaddr->ipaddr);
dad_ns++;
etimer_set(&uip_netif_timer_dad, uip_netif_physical_if.retrans_timer/1000*CLOCK_SECOND);
return;
}
/*
* If we arrive here it means DAD succeeded, otherwise the dad process
* would have been interrupted in nd6_dad_ns/na_input
*/
PRINTF("DAD succeeded for ipaddr:");
PRINT6ADDR(&(dad_ifaddr->ipaddr));
PRINTF("\n");
etimer_stop(&uip_netif_timer_dad);
dad_ifaddr->state = PREFERRED;
dad_ifaddr = NULL;
dad_ns = 0;
/*
* check if we need to run DAD on another address
* This is necessary because if you receive a RA,
* you might want to run DAD for several addresses
* Considering that we have structures to do DAD
* for one address only, we start DAD for the subsequent
* addresses here
*/
PRINTF("END of DAD\n");
for(i = 0; i < UIP_CONF_NETIF_MAX_ADDRESSES; i ++){
if(uip_netif_physical_if.addresses[i].state != NOT_USED){
PRINTF("address %d : ",i);
PRINT6ADDR(&(uip_netif_physical_if.addresses[i].ipaddr));
PRINTF("\n");
}
if(uip_netif_physical_if.addresses[i].state == TENTATIVE){
uip_netif_sched_dad(&uip_netif_physical_if.addresses[i]);
return;
}
}
}
/*---------------------------------------------------------------------------*/
void
uip_netif_dad_failed(uip_ipaddr_t *ipaddr)
{
UIP_LOG("DAD FAILED");
UIP_LOG("THE STACK IS GOING TO SHUT DOWN");
UIP_LOG("THE HOST WILL BE UNREACHABLE");
if(uip_ipaddr_cmp(&dad_ifaddr->ipaddr, ipaddr)){
etimer_stop(&uip_netif_timer_dad);
dad_ifaddr->state = NOT_USED;
dad_ifaddr = NULL;
dad_ns = 0;
}
exit(-1);
}
/*---------------------------------------------------------------------------*/
void
uip_netif_sched_send_rs(void)
{
/* before a host sends an initial solicitation, it SHOULD delay the
transmission for a random amount of time between 0 and
UIP_ND6_MAX_RTR_SOLICITATION_DELAY. */
if(rs_count == 0){
etimer_set(&uip_netif_timer_rs, random_rand()%(UIP_ND6_MAX_RTR_SOLICITATION_DELAY*CLOCK_SECOND));
PRINTF("Scheduling RS\n");
}
}
/*---------------------------------------------------------------------------*/
void
uip_netif_send_rs(void)
{
if((uip_nd6_choose_defrouter() == NULL) && (rs_count < UIP_ND6_MAX_RTR_SOLICITATIONS)){
//PRINTF("Sending RS %d\n", rs_count);
uip_nd6_io_rs_output();
rs_count++;
etimer_set(&uip_netif_timer_rs, UIP_ND6_RTR_SOLICITATION_INTERVAL * CLOCK_SECOND);
} else {
PRINTF("Router found ? (boolean): %d\n", (uip_nd6_choose_defrouter() != NULL));
etimer_stop(&uip_netif_timer_rs);
rs_count = 0;
}
}
/** @} */

207
core/net/uip-netif.h Normal file
View File

@ -0,0 +1,207 @@
/**
* \addtogroup uip6
* @{
*/
/**
* \file
* Network interface and stateless autoconfiguration (RFC 4862)
* \author Mathilde Durvy <mdurvy@cisco.com>
* \author Julien Abeille <jabeille@cisco.com>
*
*/
/*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*
*/
#ifndef __UIP_NETIF_H__
#define __UIP_NETIF_H__
#include "net/uip-nd6.h"
#ifndef UIP_CONF_NETIF_MAX_ADDRESSES
#define UIP_CONF_NETIF_MAX_ADDRESSES 2
#endif /*UIP_CONF_NETIF_MAX_ADDRESSES*/
/**
* \brief Possible states for the address of an interface (RFC 4862 autoconf +
* NOT_USED + INFINITE)
*/
typedef enum {
NOT_USED = -1,
TENTATIVE = 0,
PREFERRED = 1,
DEPRECATED = 2, /* not needed if we don't use pliffetime in prefix struct */
} uip_netif_state;
/**
* \brief How the address was acquired: Autoconf, DHCP or manually
*
*/
typedef enum {
AUTOCONF = 1,
DHCP = 2,
MANUAL = 3
} uip_netif_type;
/**
* \brief An address structure for an interface
*
* Contains an ip address assigned to the interface, and its state.
*/
struct uip_netif_addr {
uip_ipaddr_t ipaddr;
uip_netif_state state;
struct timer vlifetime;
u8_t is_infinite;
uip_netif_type type;
};
/** \brief Interface structure (contains all the interface variables) */
struct uip_netif {
u32_t link_mtu;
u8_t cur_hop_limit;
u32_t base_reachable_time; /* in msec */
u32_t reachable_time; /* in msec */
u32_t retrans_timer; /* in msec */
u8_t dup_addr_detect_transmit;
/** Note: the link-local address is at position 0 */
struct uip_netif_addr addresses[UIP_CONF_NETIF_MAX_ADDRESSES];
uip_ipaddr_t solicited_node_mcastaddr;
};
/*---------------------------------------------------------------------------*/
extern struct uip_netif uip_netif_physical_if;
extern struct etimer uip_netif_timer_dad;
extern struct etimer uip_netif_timer_rs;
extern struct etimer uip_netif_timer_periodic;
/*---------------------------------------------------------------------------*/
/** \brief Initialize the network interfac and run stateless autoconf */
void uip_netif_init(void);
/**
* \brief periodically check the state of the addresses.
*/
void uip_netif_periodic(void);
/**
* \brief recompute random reachable timer
* \return the new value for timer
*/
u32_t uip_netif_compute_reachable_time(void);
/**
* \brief Check if an unicast address is attached to my interface
* \param ipaddr an IP address to be checked
* \return 1 if address is attached to my interface (otherwise false)
*/
#define uip_netif_is_addr_my_unicast(a) (uip_netif_addr_lookup(a, 128, 0) != NULL)
/**
* \brief Check if this is my solicited-node multicast address
* \param ipaddr an IP address to be checked
* \return 1 if the address is my solicited-node (otherwise false)
*/
u8_t uip_netif_is_addr_my_solicited(uip_ipaddr_t *ipaddr);
/**
* \brief Autoconfigure and add an address corresponding to a specific prefix
* \param ipaddr the prefix if we are doing autoconf, the address for DHCP and manual
* \param length the prefix length if autoconf, 0 for DHCP and manual
* \param vlifetime valid lifetime of the address, 0 if the address has INFINITE lifetime,
* non 0 otherwise
* \param type AUTOCONF or MANUAL or DHCP
*/
void uip_netif_addr_add(uip_ipaddr_t *ipaddr, u8_t length, clock_time_t vlifetime, uip_netif_type type);
/**
* \brief Set the 8 last bytes of the IP address
* based on the L2 identifier using autoconf
* \param *ipaddr the IP address to be completed with layer 2 info
* \param *lladdr the L2 address
*/
void uip_netif_addr_autoconf_set(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr);
/**
* \brief Lookup an address
* \param ipaddr the prefix if we are looking for an autoconf address, the address otherwise
* \param length the prefix length if we are looking for autoconf address, 128 otherwise
* \param type AUTOCONF or MANUAL or DHCP or 0
*
*
* If we are looking for an AUTOCONFIGURED address, ipaddr is a prefix
* length is its length, type is AUTOCONF.
* Otherwise ipaddr is a full address, length must be 128, type is MANUAL
* or DHCP.
* Note: if we do not care about the type, type MUST be 0
*/
struct uip_netif_addr *
uip_netif_addr_lookup(uip_ipaddr_t *ipaddr, u8_t length, uip_netif_type type);
/**
* \brief Select the appropriate source address for a packet
* \param ipaddr the selected address (returned value)
* \param ipaddr the destination of the packet
*/
void uip_netif_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst);
/**
* \brief Schedule DAD for a given address
* \param ifaddr the address for which schedule DAD
*/
void uip_netif_sched_dad(struct uip_netif_addr *ifaddr);
/**
* \brief Perform DAD (i.e. Duplicate Address Detection)
*/
void uip_netif_dad(void);
/**
* \brief DAD failed, should never happen!
* \param ipaddr the address for which DAD failed
*/
void uip_netif_dad_failed(uip_ipaddr_t *ipaddr);
/**
* \brief Schedule the sending of RS
*/
void uip_netif_sched_send_rs(void);
/**
* \brief Send up to MAX_RTR_SOLICITATION_DELAY RS separated by a delay of
* RTR_SOLICITATION_INTERVAL
*/
void uip_netif_send_rs(void);
#endif
/** @} */

2108
core/net/uip6.c Normal file

File diff suppressed because it is too large Load Diff