Contiki-Inga 3.x
uip-ds6.h
1 /*
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions
5  * are met:
6  * 1. Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * 2. Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * 3. Neither the name of the Institute nor the names of its contributors
12  * may be used to endorse or promote products derived from this software
13  * without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  *
28  */
29 
30 /**
31  * \addtogroup uip6
32  * @{
33  */
34 
35 /**
36  * \file Network interface and stateless autoconfiguration (RFC 4862)
37  *
38  * \author Mathilde Durvy <mdurvy@cisco.com>
39  * \author Julien Abeille <jabeille@cisco.com>
40  */
41 
42 /**
43  * @defgroup uip_ds6 Network interface and stateless autoconfiguration (RFC 4862)
44  * @{ */
45 #ifndef UIP_DS6_H_
46 #define UIP_DS6_H_
47 
48 #include "net/ip/uip.h"
49 #include "sys/stimer.h"
50 /* The size of uip_ds6_addr_t depends on UIP_ND6_DEF_MAXDADNS. Include uip-nd6.h to define it. */
51 #include "net/ipv6/uip-nd6.h"
52 #include "net/ipv6/uip-ds6-route.h"
53 #include "net/ipv6/uip-ds6-nbr.h"
54 
55 /*--------------------------------------------------*/
56 /** Configuration. For all tables (Neighbor cache, Prefix List, Routing Table,
57  * Default Router List, Unicast address list, multicast address list, anycast address list),
58  * we define:
59  * - the number of elements requested by the user in contiki configuration (name suffixed by _NBU)
60  * - the number of elements assigned by the system (name suffixed by _NBS)
61  * - the total number of elements is the sum (name suffixed by _NB)
62 */
63 
64 /* Default router list */
65 #define UIP_DS6_DEFRT_NBS 0
66 #ifndef UIP_CONF_DS6_DEFRT_NBU
67 #define UIP_DS6_DEFRT_NBU 2
68 #else
69 #define UIP_DS6_DEFRT_NBU UIP_CONF_DS6_DEFRT_NBU
70 #endif
71 #define UIP_DS6_DEFRT_NB UIP_DS6_DEFRT_NBS + UIP_DS6_DEFRT_NBU
72 
73 /* Prefix list */
74 #define UIP_DS6_PREFIX_NBS 1
75 #ifndef UIP_CONF_DS6_PREFIX_NBU
76 #define UIP_DS6_PREFIX_NBU 2
77 #else
78 #define UIP_DS6_PREFIX_NBU UIP_CONF_DS6_PREFIX_NBU
79 #endif
80 #define UIP_DS6_PREFIX_NB UIP_DS6_PREFIX_NBS + UIP_DS6_PREFIX_NBU
81 
82 /* Unicast address list*/
83 #define UIP_DS6_ADDR_NBS 1
84 #ifndef UIP_CONF_DS6_ADDR_NBU
85 #define UIP_DS6_ADDR_NBU 2
86 #else
87 #define UIP_DS6_ADDR_NBU UIP_CONF_DS6_ADDR_NBU
88 #endif
89 #define UIP_DS6_ADDR_NB UIP_DS6_ADDR_NBS + UIP_DS6_ADDR_NBU
90 
91 /* Multicast address list */
92 #if UIP_CONF_ROUTER
93 #define UIP_DS6_MADDR_NBS 2 + UIP_DS6_ADDR_NB /* all routers + all nodes + one solicited per unicast */
94 #else
95 #define UIP_DS6_MADDR_NBS 1 + UIP_DS6_ADDR_NB /* all nodes + one solicited per unicast */
96 #endif
97 #ifndef UIP_CONF_DS6_MADDR_NBU
98 #define UIP_DS6_MADDR_NBU 0
99 #else
100 #define UIP_DS6_MADDR_NBU UIP_CONF_DS6_MADDR_NBU
101 #endif
102 #define UIP_DS6_MADDR_NB UIP_DS6_MADDR_NBS + UIP_DS6_MADDR_NBU
103 
104 /* Anycast address list */
105 #if UIP_CONF_ROUTER
106 #define UIP_DS6_AADDR_NBS UIP_DS6_PREFIX_NB - 1 /* One per non link local prefix (subnet prefix anycast address) */
107 #else
108 #define UIP_DS6_AADDR_NBS 0
109 #endif
110 #ifndef UIP_CONF_DS6_AADDR_NBU
111 #define UIP_DS6_AADDR_NBU 0
112 #else
113 #define UIP_DS6_AADDR_NBU UIP_CONF_DS6_AADDR_NBU
114 #endif
115 #define UIP_DS6_AADDR_NB UIP_DS6_AADDR_NBS + UIP_DS6_AADDR_NBU
116 
117 /*--------------------------------------------------*/
118 /* Should we use LinkLayer acks in NUD ?*/
119 #ifndef UIP_CONF_DS6_LL_NUD
120 #define UIP_DS6_LL_NUD 0
121 #else
122 #define UIP_DS6_LL_NUD UIP_CONF_DS6_LL_NUD
123 #endif
124 
125 /** \name Possible states for the an address (RFC 4862)
126  * @{ */
127 #define ADDR_TENTATIVE 0
128 #define ADDR_PREFERRED 1
129 #define ADDR_DEPRECATED 2
130 /** @} */
131 
132 /** \name How the address was acquired: Autoconf, DHCP or manually
133  * @{ */
134 #define ADDR_ANYTYPE 0
135 #define ADDR_AUTOCONF 1
136 #define ADDR_DHCP 2
137 #define ADDR_MANUAL 3
138 /** @} */
139 
140 /** \name General DS6 definitions
141  * @{ */
142 #define UIP_DS6_PERIOD (CLOCK_SECOND/10) /** Period for uip-ds6 periodic task*/
143 #define FOUND 0
144 #define FREESPACE 1
145 #define NOSPACE 2
146 /** @} */
147 /*--------------------------------------------------*/
148 
149 #if UIP_CONF_IPV6_QUEUE_PKT
150 #include "net/ip/uip-packetqueue.h"
151 #endif /*UIP_CONF_QUEUE_PKT */
152 
153 /** \brief A prefix list entry */
154 #if UIP_CONF_ROUTER
155 typedef struct uip_ds6_prefix {
156  uint8_t isused;
157  uip_ipaddr_t ipaddr;
158  uint8_t length;
159  uint8_t advertise;
160  uint32_t vlifetime;
161  uint32_t plifetime;
162  uint8_t l_a_reserved; /**< on-link and autonomous flags + 6 reserved bits */
164 #else /* UIP_CONF_ROUTER */
165 typedef struct uip_ds6_prefix {
166  uint8_t isused;
167  uip_ipaddr_t ipaddr;
168  uint8_t length;
169  struct stimer vlifetime;
170  uint8_t isinfinite;
172 #endif /*UIP_CONF_ROUTER */
173 
174 /** \brief Unicast address structure */
175 typedef struct uip_ds6_addr {
176  uint8_t isused;
177  uip_ipaddr_t ipaddr;
178  uint8_t state;
179  uint8_t type;
180  uint8_t isinfinite;
181  struct stimer vlifetime;
182 #if UIP_ND6_DEF_MAXDADNS > 0
183  struct timer dadtimer;
184  uint8_t dadnscount;
185 #endif /* UIP_ND6_DEF_MAXDADNS > 0 */
187 
188 /** \brief Anycast address */
189 typedef struct uip_ds6_aaddr {
190  uint8_t isused;
191  uip_ipaddr_t ipaddr;
193 
194 /** \brief A multicast address */
195 typedef struct uip_ds6_maddr {
196  uint8_t isused;
197  uip_ipaddr_t ipaddr;
199 
200 /* only define the callback if RPL is active */
201 #if UIP_CONF_IPV6_RPL
202 #ifndef UIP_CONF_DS6_NEIGHBOR_STATE_CHANGED
203 #define UIP_CONF_DS6_NEIGHBOR_STATE_CHANGED rpl_ipv6_neighbor_callback
204 #endif /* UIP_CONF_DS6_NEIGHBOR_STATE_CHANGED */
205 #endif /* UIP_CONF_IPV6_RPL */
206 
207 #if UIP_CONF_IPV6_RPL
208 #ifndef UIP_CONF_DS6_LINK_NEIGHBOR_CALLBACK
209 #define UIP_CONF_DS6_LINK_NEIGHBOR_CALLBACK rpl_link_neighbor_callback
210 #endif /* UIP_CONF_DS6_NEIGHBOR_STATE_CHANGED */
211 #endif /* UIP_CONF_IPV6_RPL */
212 
213 
214 /** \brief Interface structure (contains all the interface variables) */
215 typedef struct uip_ds6_netif {
216  uint32_t link_mtu;
217  uint8_t cur_hop_limit;
218  uint32_t base_reachable_time; /* in msec */
219  uint32_t reachable_time; /* in msec */
220  uint32_t retrans_timer; /* in msec */
221  uint8_t maxdadns;
222  uip_ds6_addr_t addr_list[UIP_DS6_ADDR_NB];
223  uip_ds6_aaddr_t aaddr_list[UIP_DS6_AADDR_NB];
224  uip_ds6_maddr_t maddr_list[UIP_DS6_MADDR_NB];
226 
227 /** \brief Generic type for a DS6, to use a common loop though all DS */
228 typedef struct uip_ds6_element {
229  uint8_t isused;
230  uip_ipaddr_t ipaddr;
232 
233 
234 /*---------------------------------------------------------------------------*/
235 extern uip_ds6_netif_t uip_ds6_if;
236 extern struct etimer uip_ds6_timer_periodic;
237 
238 #if UIP_CONF_ROUTER
239 extern uip_ds6_prefix_t uip_ds6_prefix_list[UIP_DS6_PREFIX_NB];
240 #else /* UIP_CONF_ROUTER */
241 extern struct etimer uip_ds6_timer_rs;
242 #endif /* UIP_CONF_ROUTER */
243 
244 
245 /*---------------------------------------------------------------------------*/
246 /** \brief Initialize data structures */
247 void uip_ds6_init(void);
248 
249 /** \brief Periodic processing of data structures */
250 void uip_ds6_periodic(void);
251 
252 /** \brief Generic loop routine on an abstract data structure, which generalizes
253  * all data structures used in DS6 */
254 uint8_t uip_ds6_list_loop(uip_ds6_element_t *list, uint8_t size,
255  uint16_t elementsize, uip_ipaddr_t *ipaddr,
256  uint8_t ipaddrlen,
257  uip_ds6_element_t **out_element);
258 
259 
260 
261 /** \name Prefix list basic routines
262  * @{ */
263 #if UIP_CONF_ROUTER
264 /** Adds a prefix.
265  *
266  * @param ipaddr
267  * @param length length of subnet prefix (bits)?
268  * @param advertise True, if the prefix should be advertised by the router (RA)?
269  * @param flags Flags
270  * @param vtime Valid lifetime
271  * @param ptime Preferred lifetime
272  */
273 uip_ds6_prefix_t *uip_ds6_prefix_add(uip_ipaddr_t *ipaddr, uint8_t length,
274  uint8_t advertise, uint8_t flags,
275  unsigned long vtime,
276  unsigned long ptime);
277 #else /* UIP_CONF_ROUTER */
278 uip_ds6_prefix_t *uip_ds6_prefix_add(uip_ipaddr_t *ipaddr, uint8_t length,
279  unsigned long interval);
280 #endif /* UIP_CONF_ROUTER */
281 void uip_ds6_prefix_rm(uip_ds6_prefix_t *prefix);
282 uip_ds6_prefix_t *uip_ds6_prefix_lookup(uip_ipaddr_t *ipaddr,
283  uint8_t ipaddrlen);
284 uint8_t uip_ds6_is_addr_onlink(uip_ipaddr_t *ipaddr);
285 /** @} */
286 
287 /** \name Unicast address list basic routines
288  * @{ */
289 uip_ds6_addr_t *uip_ds6_addr_add(uip_ipaddr_t *ipaddr,
290  unsigned long vlifetime, uint8_t type);
291 void uip_ds6_addr_rm(uip_ds6_addr_t *addr);
292 uip_ds6_addr_t *uip_ds6_addr_lookup(uip_ipaddr_t *ipaddr);
293 uip_ds6_addr_t *uip_ds6_get_link_local(int8_t state);
294 uip_ds6_addr_t *uip_ds6_get_global(int8_t state);
295 /** @} */
296 
297 /** \name Multicast address list basic routines
298  * @{ */
299 uip_ds6_maddr_t *uip_ds6_maddr_add(const uip_ipaddr_t *ipaddr);
300 void uip_ds6_maddr_rm(uip_ds6_maddr_t *maddr);
301 uip_ds6_maddr_t *uip_ds6_maddr_lookup(const uip_ipaddr_t *ipaddr);
302 /** @} */
303 
304 /** \name Anycast address list basic routines
305  * @{ */
306 uip_ds6_aaddr_t *uip_ds6_aaddr_add(uip_ipaddr_t *ipaddr);
307 void uip_ds6_aaddr_rm(uip_ds6_aaddr_t *aaddr);
308 uip_ds6_aaddr_t *uip_ds6_aaddr_lookup(uip_ipaddr_t *ipaddr);
309 /** @} */
310 
311 
312 /** \brief set the last 64 bits of an IP address based on the MAC address */
313 void uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr);
314 
315 /** \brief Get the number of matching bits of two addresses */
316 uint8_t get_match_length(uip_ipaddr_t *src, uip_ipaddr_t *dst);
317 
318 #if UIP_ND6_DEF_MAXDADNS >0
319 /** \brief Perform Duplicate Address Selection on one address */
320 void uip_ds6_dad(uip_ds6_addr_t *ifaddr);
321 
322 /** \brief Callback when DAD failed */
323 int uip_ds6_dad_failed(uip_ds6_addr_t *ifaddr);
324 #endif /* UIP_ND6_DEF_MAXDADNS */
325 
326 /** \brief Source address selection, see RFC 3484 */
327 void uip_ds6_select_src(uip_ipaddr_t *src, uip_ipaddr_t *dst);
328 
329 #if UIP_CONF_ROUTER
330 #if UIP_ND6_SEND_RA
331 /** \brief Send a RA as an asnwer to a RS */
332 void uip_ds6_send_ra_sollicited(void);
333 
334 /** \brief Send a periodic RA */
335 void uip_ds6_send_ra_periodic(void);
336 #endif /* UIP_ND6_SEND_RA */
337 #else /* UIP_CONF_ROUTER */
338 /** \brief Send periodic RS to find router */
339 void uip_ds6_send_rs(void);
340 #endif /* UIP_CONF_ROUTER */
341 
342 /** \brief Compute the reachable time based on base reachable time, see RFC 4861*/
343 uint32_t uip_ds6_compute_reachable_time(void); /** \brief compute random reachable timer */
344 
345 /** \name Macros to check if an IP address (unicast, multicast or anycast) is mine */
346 /** @{ */
347 /** Checks if IP address is mine. */
348 #define uip_ds6_is_my_addr(addr) (uip_ds6_addr_lookup(addr) != NULL)
349 /** Checks if IP multicaste address is mine. */
350 #define uip_ds6_is_my_maddr(addr) (uip_ds6_maddr_lookup(addr) != NULL)
351 /** Checks if IP anycast address is mine. */
352 #define uip_ds6_is_my_aaddr(addr) (uip_ds6_aaddr_lookup(addr) != NULL)
353 /** @} */
354 
355 /** @} */
356 /** @} */
357 
358 #endif /* UIP_DS6_H_ */