Contiki 2.6

uip-ds6.h

Go to the documentation of this file.
00001 /**
00002  * \addtogroup uip6
00003  * @{
00004  */
00005 
00006 /**
00007  * \file
00008  *         Network interface and stateless autoconfiguration (RFC 4862)
00009  * \author Mathilde Durvy <mdurvy@cisco.com>
00010  * \author Julien Abeille <jabeille@cisco.com>
00011  *
00012  */
00013 /*
00014  *
00015  * Redistribution and use in source and binary forms, with or without
00016  * modification, are permitted provided that the following conditions
00017  * are met:
00018  * 1. Redistributions of source code must retain the above copyright
00019  *    notice, this list of conditions and the following disclaimer.
00020  * 2. Redistributions in binary form must reproduce the above copyright
00021  *    notice, this list of conditions and the following disclaimer in the
00022  *    documentation and/or other materials provided with the distribution.
00023  * 3. Neither the name of the Institute nor the names of its contributors
00024  *    may be used to endorse or promote products derived from this software
00025  *    without specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00028  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00031  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00032  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00033  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00034  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00035  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00036  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00037  * SUCH DAMAGE.
00038  *
00039  *
00040  */
00041 
00042 #ifndef __UIP_DS6_H__
00043 #define __UIP_DS6_H__
00044 
00045 #include "net/uip.h"
00046 #include "sys/stimer.h"
00047 /* The size of uip_ds6_addr_t depends on UIP_ND6_DEF_MAXDADNS. Include uip-nd6.h to define it. */
00048 #include "net/uip-nd6.h"
00049 
00050 /*--------------------------------------------------*/
00051 /** Configuration. For all tables (Neighbor cache, Prefix List, Routing Table,
00052  * Default Router List, Unicast address list, multicast address list, anycast address list),
00053  * we define:
00054  * - the number of elements requested by the user in contiki configuration (name suffixed by _NBU)
00055  * - the number of elements assigned by the system (name suffixed by _NBS)
00056  * - the total number of elements is the sum (name suffixed by _NB)
00057 */
00058 /* Neighbor cache */
00059 #define UIP_DS6_NBR_NBS 0
00060 #ifndef UIP_CONF_DS6_NBR_NBU
00061 #define UIP_DS6_NBR_NBU  4
00062 #else
00063 #define UIP_DS6_NBR_NBU UIP_CONF_DS6_NBR_NBU
00064 #endif
00065 #define UIP_DS6_NBR_NB UIP_DS6_NBR_NBS + UIP_DS6_NBR_NBU
00066 
00067 /* Default router list */
00068 #define UIP_DS6_DEFRT_NBS 0
00069 #ifndef UIP_CONF_DS6_DEFRT_NBU
00070 #define UIP_DS6_DEFRT_NBU 2
00071 #else
00072 #define UIP_DS6_DEFRT_NBU UIP_CONF_DS6_DEFRT_NBU
00073 #endif
00074 #define UIP_DS6_DEFRT_NB UIP_DS6_DEFRT_NBS + UIP_DS6_DEFRT_NBU
00075 
00076 /* Prefix list */
00077 #define UIP_DS6_PREFIX_NBS  1
00078 #ifndef UIP_CONF_DS6_PREFIX_NBU
00079 #define UIP_DS6_PREFIX_NBU  2
00080 #else
00081 #define UIP_DS6_PREFIX_NBU UIP_CONF_DS6_PREFIX_NBU
00082 #endif
00083 #define UIP_DS6_PREFIX_NB UIP_DS6_PREFIX_NBS + UIP_DS6_PREFIX_NBU
00084 
00085 /* Routing table */
00086 #define UIP_DS6_ROUTE_NBS 0
00087 #ifndef UIP_CONF_DS6_ROUTE_NBU
00088 #define UIP_DS6_ROUTE_NBU 4
00089 #else
00090 #define UIP_DS6_ROUTE_NBU UIP_CONF_DS6_ROUTE_NBU
00091 #endif
00092 #define UIP_DS6_ROUTE_NB UIP_DS6_ROUTE_NBS + UIP_DS6_ROUTE_NBU
00093 
00094 /* Unicast address list*/
00095 #define UIP_DS6_ADDR_NBS 1
00096 #ifndef UIP_CONF_DS6_ADDR_NBU
00097 #define UIP_DS6_ADDR_NBU 2
00098 #else
00099 #define UIP_DS6_ADDR_NBU UIP_CONF_DS6_ADDR_NBU
00100 #endif
00101 #define UIP_DS6_ADDR_NB UIP_DS6_ADDR_NBS + UIP_DS6_ADDR_NBU
00102 
00103 /* Multicast address list */
00104 #if UIP_CONF_ROUTER
00105 #define UIP_DS6_MADDR_NBS 2 + UIP_DS6_ADDR_NB   /* all routers + all nodes + one solicited per unicast */
00106 #else
00107 #define UIP_DS6_MADDR_NBS 1 + UIP_DS6_ADDR_NB   /* all nodes + one solicited per unicast */
00108 #endif
00109 #ifndef UIP_CONF_DS6_MADDR_NBU
00110 #define UIP_DS6_MADDR_NBU 0
00111 #else
00112 #define UIP_DS6_MADDR_NBU UIP_CONF_DS6_MADDR_NBU
00113 #endif
00114 #define UIP_DS6_MADDR_NB UIP_DS6_MADDR_NBS + UIP_DS6_MADDR_NBU
00115 
00116 /* Anycast address list */
00117 #if UIP_CONF_ROUTER
00118 #define UIP_DS6_AADDR_NBS UIP_DS6_PREFIX_NB - 1 /* One per non link local prefix (subnet prefix anycast address) */
00119 #else
00120 #define UIP_DS6_AADDR_NBS 0
00121 #endif
00122 #ifndef UIP_CONF_DS6_AADDR_NBU
00123 #define UIP_DS6_AADDR_NBU 0
00124 #else
00125 #define UIP_DS6_AADDR_NBU UIP_CONF_DS6_AADDR_NBU
00126 #endif
00127 #define UIP_DS6_AADDR_NB UIP_DS6_AADDR_NBS + UIP_DS6_AADDR_NBU
00128 
00129 /*--------------------------------------------------*/
00130 /* Should we use LinkLayer acks in NUD ?*/
00131 #ifndef UIP_CONF_DS6_LL_NUD
00132 #define UIP_DS6_LL_NUD 0
00133 #else
00134 #define UIP_DS6_LL_NUD UIP_CONF_DS6_LL_NUD
00135 #endif
00136 
00137 /*--------------------------------------------------*/
00138 /** \brief Possible states for the nbr cache entries */
00139 #define  NBR_INCOMPLETE 0
00140 #define  NBR_REACHABLE 1
00141 #define  NBR_STALE 2
00142 #define  NBR_DELAY 3
00143 #define  NBR_PROBE 4
00144 
00145 /** \brief Possible states for the an address  (RFC 4862) */
00146 #define ADDR_TENTATIVE 0
00147 #define ADDR_PREFERRED 1
00148 #define ADDR_DEPRECATED 2
00149 
00150 /** \brief How the address was acquired: Autoconf, DHCP or manually */
00151 #define  ADDR_ANYTYPE 0
00152 #define  ADDR_AUTOCONF 1
00153 #define  ADDR_DHCP 2
00154 #define  ADDR_MANUAL 3
00155 
00156 /** \brief General DS6 definitions */
00157 #define UIP_DS6_PERIOD   (CLOCK_SECOND/10)  /** Period for uip-ds6 periodic task*/
00158 #define FOUND 0
00159 #define FREESPACE 1
00160 #define NOSPACE 2
00161 
00162 
00163 /*--------------------------------------------------*/
00164 #if UIP_CONF_IPV6_QUEUE_PKT
00165 #include "net/uip-packetqueue.h"
00166 #endif                          /*UIP_CONF_QUEUE_PKT */
00167 /** \brief An entry in the nbr cache */
00168 typedef struct uip_ds6_nbr {
00169   uint8_t isused;
00170   uip_ipaddr_t ipaddr;
00171   uip_lladdr_t lladdr;
00172   struct stimer reachable;
00173   struct stimer sendns;
00174   clock_time_t last_lookup;
00175   uint8_t nscount;
00176   uint8_t isrouter;
00177   uint8_t state;
00178 #if UIP_CONF_IPV6_QUEUE_PKT
00179   struct uip_packetqueue_handle packethandle;
00180 #define UIP_DS6_NBR_PACKET_LIFETIME CLOCK_SECOND * 4
00181 #endif                          /*UIP_CONF_QUEUE_PKT */
00182 } uip_ds6_nbr_t;
00183 
00184 /** \brief An entry in the default router list */
00185 typedef struct uip_ds6_defrt {
00186   uint8_t isused;
00187   uip_ipaddr_t ipaddr;
00188   struct stimer lifetime;
00189   uint8_t isinfinite;
00190 } uip_ds6_defrt_t;
00191 
00192 /** \brief A prefix list entry */
00193 #if UIP_CONF_ROUTER
00194 typedef struct uip_ds6_prefix {
00195   uint8_t isused;
00196   uip_ipaddr_t ipaddr;
00197   uint8_t length;
00198   uint8_t advertise;
00199   uint32_t vlifetime;
00200   uint32_t plifetime;
00201   uint8_t l_a_reserved; /**< on-link and autonomous flags + 6 reserved bits */
00202 } uip_ds6_prefix_t;
00203 #else /* UIP_CONF_ROUTER */
00204 typedef struct uip_ds6_prefix {
00205   uint8_t isused;
00206   uip_ipaddr_t ipaddr;
00207   uint8_t length;
00208   struct stimer vlifetime;
00209   uint8_t isinfinite;
00210 } uip_ds6_prefix_t;
00211 #endif /*UIP_CONF_ROUTER */
00212 
00213 /** * \brief Unicast address structure */
00214 typedef struct uip_ds6_addr {
00215   uint8_t isused;
00216   uip_ipaddr_t ipaddr;
00217   uint8_t state;
00218   uint8_t type;
00219   uint8_t isinfinite;
00220   struct stimer vlifetime;
00221 #if UIP_ND6_DEF_MAXDADNS > 0
00222   struct timer dadtimer;
00223   uint8_t dadnscount;
00224 #endif /* UIP_ND6_DEF_MAXDADNS > 0 */
00225 } uip_ds6_addr_t;
00226 
00227 /** \brief Anycast address  */
00228 typedef struct uip_ds6_aaddr {
00229   uint8_t isused;
00230   uip_ipaddr_t ipaddr;
00231 } uip_ds6_aaddr_t;
00232 
00233 /** \brief A multicast address */
00234 typedef struct uip_ds6_maddr {
00235   uint8_t isused;
00236   uip_ipaddr_t ipaddr;
00237 } uip_ds6_maddr_t;
00238 
00239 /** \brief define some additional RPL related route state and
00240  *  neighbor callback for RPL - if not a DS6_ROUTE_STATE is already set */
00241 #ifndef UIP_DS6_ROUTE_STATE_TYPE
00242 #define UIP_DS6_ROUTE_STATE_TYPE rpl_route_entry_t
00243 /* Needed for the extended route entry state when using ContikiRPL */
00244 typedef struct rpl_route_entry {
00245   uint32_t lifetime;
00246   uint32_t saved_lifetime;
00247   void *dag;
00248   uint8_t learned_from;
00249 } rpl_route_entry_t;
00250 #endif /* UIP_DS6_ROUTE_STATE_TYPE */
00251 
00252 /* only define the callback if RPL is active */
00253 #if UIP_CONF_IPV6_RPL
00254 #ifndef UIP_CONF_DS6_NEIGHBOR_STATE_CHANGED
00255 #define UIP_CONF_DS6_NEIGHBOR_STATE_CHANGED rpl_ipv6_neighbor_callback
00256 #endif /* UIP_CONF_DS6_NEIGHBOR_STATE_CHANGED */
00257 #endif /* UIP_CONF_IPV6_RPL */
00258 
00259 
00260 
00261 /** \brief An entry in the routing table */
00262 typedef struct uip_ds6_route {
00263   uint8_t isused;
00264   uip_ipaddr_t ipaddr;
00265   uint8_t length;
00266   uint8_t metric;
00267   uip_ipaddr_t nexthop;
00268 #ifdef UIP_DS6_ROUTE_STATE_TYPE
00269   UIP_DS6_ROUTE_STATE_TYPE state;
00270 #endif
00271 } uip_ds6_route_t;
00272 
00273 /** \brief  Interface structure (contains all the interface variables) */
00274 typedef struct uip_ds6_netif {
00275   uint32_t link_mtu;
00276   uint8_t cur_hop_limit;
00277   uint32_t base_reachable_time; /* in msec */
00278   uint32_t reachable_time;      /* in msec */
00279   uint32_t retrans_timer;       /* in msec */
00280   uint8_t maxdadns;
00281   uip_ds6_addr_t addr_list[UIP_DS6_ADDR_NB];
00282   uip_ds6_aaddr_t aaddr_list[UIP_DS6_AADDR_NB];
00283   uip_ds6_maddr_t maddr_list[UIP_DS6_MADDR_NB];
00284 } uip_ds6_netif_t;
00285 
00286 /** \brief Generic type for a DS6, to use a common loop though all DS */
00287 typedef struct uip_ds6_element {
00288   uint8_t isused;
00289   uip_ipaddr_t ipaddr;
00290 } uip_ds6_element_t;
00291 
00292 
00293 /*---------------------------------------------------------------------------*/
00294 extern uip_ds6_netif_t uip_ds6_if;
00295 extern struct etimer uip_ds6_timer_periodic;
00296 
00297 #if UIP_CONF_ROUTER
00298 extern uip_ds6_prefix_t uip_ds6_prefix_list[UIP_DS6_PREFIX_NB];
00299 #else /* UIP_CONF_ROUTER */
00300 extern struct etimer uip_ds6_timer_rs;
00301 #endif /* UIP_CONF_ROUTER */
00302 
00303 
00304 /*---------------------------------------------------------------------------*/
00305 /** \brief Initialize data structures */
00306 void uip_ds6_init(void);
00307 
00308 /** \brief Periodic processing of data structures */
00309 void uip_ds6_periodic(void);
00310 
00311 /** \brief Generic loop routine on an abstract data structure, which generalizes
00312  * all data structures used in DS6 */
00313 uint8_t uip_ds6_list_loop(uip_ds6_element_t *list, uint8_t size,
00314                           uint16_t elementsize, uip_ipaddr_t *ipaddr,
00315                           uint8_t ipaddrlen,
00316                           uip_ds6_element_t **out_element);
00317 
00318 /** \name Neighbor Cache basic routines */
00319 /** @{ */
00320 uip_ds6_nbr_t *uip_ds6_nbr_add(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr,
00321                                uint8_t isrouter, uint8_t state);
00322 void uip_ds6_nbr_rm(uip_ds6_nbr_t *nbr);
00323 uip_ds6_nbr_t *uip_ds6_nbr_lookup(uip_ipaddr_t *ipaddr);
00324 uip_ds6_nbr_t *uip_ds6_nbr_ll_lookup(uip_lladdr_t *lladdr);
00325 
00326 /** @} */
00327 
00328 /** \name Default router list basic routines */
00329 /** @{ */
00330 uip_ds6_defrt_t *uip_ds6_defrt_add(uip_ipaddr_t *ipaddr,
00331                                    unsigned long interval);
00332 void uip_ds6_defrt_rm(uip_ds6_defrt_t *defrt);
00333 uip_ds6_defrt_t *uip_ds6_defrt_lookup(uip_ipaddr_t *ipaddr);
00334 uip_ipaddr_t *uip_ds6_defrt_choose(void);
00335 
00336 /** @} */
00337 
00338 /** \name Prefix list basic routines */
00339 /** @{ */
00340 #if UIP_CONF_ROUTER
00341 uip_ds6_prefix_t *uip_ds6_prefix_add(uip_ipaddr_t *ipaddr, uint8_t length,
00342                                      uint8_t advertise, uint8_t flags,
00343                                      unsigned long vtime,
00344                                      unsigned long ptime);
00345 #else /* UIP_CONF_ROUTER */
00346 uip_ds6_prefix_t *uip_ds6_prefix_add(uip_ipaddr_t *ipaddr, uint8_t length,
00347                                      unsigned long interval);
00348 #endif /* UIP_CONF_ROUTER */
00349 void uip_ds6_prefix_rm(uip_ds6_prefix_t *prefix);
00350 uip_ds6_prefix_t *uip_ds6_prefix_lookup(uip_ipaddr_t *ipaddr,
00351                                         uint8_t ipaddrlen);
00352 uint8_t uip_ds6_is_addr_onlink(uip_ipaddr_t *ipaddr);
00353 
00354 /** @} */
00355 
00356 /** \name Unicast address list basic routines */
00357 /** @{ */
00358 uip_ds6_addr_t *uip_ds6_addr_add(uip_ipaddr_t *ipaddr,
00359                                  unsigned long vlifetime, uint8_t type);
00360 void uip_ds6_addr_rm(uip_ds6_addr_t *addr);
00361 uip_ds6_addr_t *uip_ds6_addr_lookup(uip_ipaddr_t *ipaddr);
00362 uip_ds6_addr_t *uip_ds6_get_link_local(int8_t state);
00363 uip_ds6_addr_t *uip_ds6_get_global(int8_t state);
00364 
00365 /** @} */
00366 
00367 /** \name Multicast address list basic routines */
00368 /** @{ */
00369 uip_ds6_maddr_t *uip_ds6_maddr_add(uip_ipaddr_t *ipaddr);
00370 void uip_ds6_maddr_rm(uip_ds6_maddr_t *maddr);
00371 uip_ds6_maddr_t *uip_ds6_maddr_lookup(uip_ipaddr_t *ipaddr);
00372 
00373 /** @} */
00374 
00375 /** \name Anycast address list basic routines */
00376 /** @{ */
00377 uip_ds6_aaddr_t *uip_ds6_aaddr_add(uip_ipaddr_t *ipaddr);
00378 void uip_ds6_aaddr_rm(uip_ds6_aaddr_t *aaddr);
00379 uip_ds6_aaddr_t *uip_ds6_aaddr_lookup(uip_ipaddr_t *ipaddr);
00380 
00381 /** @} */
00382 
00383 
00384 /** \name Routing Table basic routines */
00385 /** @{ */
00386 uip_ds6_route_t *uip_ds6_route_lookup(uip_ipaddr_t *destipaddr);
00387 uip_ds6_route_t *uip_ds6_route_add(uip_ipaddr_t *ipaddr, uint8_t length,
00388                                    uip_ipaddr_t *next_hop, uint8_t metric);
00389 void uip_ds6_route_rm(uip_ds6_route_t *route);
00390 void uip_ds6_route_rm_by_nexthop(uip_ipaddr_t *nexthop);
00391 
00392 /** @} */
00393 
00394 /** \brief set the last 64 bits of an IP address based on the MAC address */
00395 void uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr);
00396 
00397 /** \brief Get the number of matching bits of two addresses */
00398 uint8_t get_match_length(uip_ipaddr_t *src, uip_ipaddr_t *dst);
00399 
00400 #if UIP_ND6_DEF_MAXDADNS >0
00401 /** \brief Perform Duplicate Address Selection on one address */
00402 void uip_ds6_dad(uip_ds6_addr_t *ifaddr);
00403 
00404 /** \brief Callback when DAD failed */
00405 int uip_ds6_dad_failed(uip_ds6_addr_t *ifaddr);
00406 #endif /* UIP_ND6_DEF_MAXDADNS */
00407 
00408 /** \brief Source address selection, see RFC 3484 */
00409 void uip_ds6_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst);
00410 
00411 #if UIP_CONF_ROUTER
00412 #if UIP_ND6_SEND_RA
00413 /** \brief Send a RA as an asnwer to a RS */
00414 void uip_ds6_send_ra_sollicited(void);
00415 
00416 /** \brief Send a periodic RA */
00417 void uip_ds6_send_ra_periodic(void);
00418 #endif /* UIP_ND6_SEND_RA */
00419 #else /* UIP_CONF_ROUTER */
00420 /** \brief Send periodic RS to find router */
00421 void uip_ds6_send_rs(void);
00422 #endif /* UIP_CONF_ROUTER */
00423 
00424 /** \brief Compute the reachable time based on base reachable time, see RFC 4861*/
00425 uint32_t uip_ds6_compute_reachable_time(void); /** \brief compute random reachable timer */
00426 
00427 /** \name Macros to check if an IP address (unicast, multicast or anycast) is mine */
00428 /** @{ */
00429 #define uip_ds6_is_my_addr(addr)  (uip_ds6_addr_lookup(addr) != NULL)
00430 #define uip_ds6_is_my_maddr(addr) (uip_ds6_maddr_lookup(addr) != NULL)
00431 #define uip_ds6_is_my_aaddr(addr) (uip_ds6_aaddr_lookup(addr) != NULL)
00432 /** @} */
00433 /** @} */
00434 
00435 #endif /* __UIP_DS6_H__ */