_nib-internal.h
1 /*
2  * Copyright (C) 2017 Freie Universit√§t Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
19 #ifndef PRIV_NIB_INTERNAL_H
20 #define PRIV_NIB_INTERNAL_H
21 
22 #include <assert.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <string.h>
26 
27 #include "bitfield.h"
28 #include "evtimer_msg.h"
29 #include "kernel_types.h"
30 #include "mutex.h"
31 #include "net/eui64.h"
32 #include "net/ipv6/addr.h"
33 #ifdef MODULE_GNRC_IPV6
34 #include "net/gnrc/ipv6.h"
35 #endif
36 #include "net/gnrc/ipv6/nib/ft.h"
37 #include "net/gnrc/ipv6/nib/nc.h"
38 #include "net/gnrc/ipv6/nib/conf.h"
39 #include "net/gnrc/pktqueue.h"
40 #include "net/gnrc/sixlowpan/ctx.h"
41 #include "net/ndp.h"
42 #include "random.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
53 #define _EMPTY (0x00)
54 #define _NC (0x01)
55 #define _DC (0x02)
56 #define _PL (0x04)
57 #define _DRL (0x08)
58 #define _FT (0x10)
59 #define _DAD (0x20)
60 #define _DST (0x40)
69 #define _PFX_ON_LINK (0x0001)
70 #define _PFX_SLAAC (0x0002)
71 
76 #define _NIB_IF_MASK (GNRC_IPV6_NIB_NC_INFO_IFACE_MASK)
77 
81 #define _NIB_IF_POS (GNRC_IPV6_NIB_NC_INFO_IFACE_POS)
82 
86 #define _NIB_IF_MAX (_NIB_IF_MASK >> _NIB_IF_POS)
87 
92 typedef struct _nib_onl_entry {
93  struct _nib_onl_entry *next;
94 #if GNRC_IPV6_NIB_CONF_QUEUE_PKT || defined(DOXYGEN)
95 
101 #endif
102 
106 #if GNRC_IPV6_NIB_CONF_6LR || defined(DOXYGEN)
107 
112  eui64_t eui64;
113 #endif
114 #if GNRC_IPV6_NIB_CONF_ARSM || defined(DOXYGEN)
115 
121 #endif
122 
144 #if GNRC_IPV6_NIB_CONF_ROUTER || defined(DOXYGEN)
146 #endif
147 #if GNRC_IPV6_NIB_CONF_6LR || defined(DOXYGEN)
149 #endif
150 
156  uint16_t info;
165  uint8_t mode;
166 #if GNRC_IPV6_NIB_CONF_ARSM || defined(DOXYGEN)
167 
172  uint8_t ns_sent;
179  uint8_t l2addr_len;
180 #endif
182 
186 typedef struct {
187  _nib_onl_entry_t *next_hop;
191  evtimer_msg_event_t rtr_timeout;
193 
197 typedef struct {
198  _nib_onl_entry_t *next_hop;
203  evtimer_msg_event_t pfx_timeout;
204 #ifdef GNRC_IPV6_NIB_CONF_ROUTER
205 
208  evtimer_msg_event_t route_timeout;
209 #endif
210  uint8_t mode;
212  uint8_t pfx_len;
214  uint16_t flags;
215  uint32_t valid_until;
217  uint32_t pref_until;
220 
225 typedef struct {
226  ipv6_addr_t addr;
227  uint32_t version;
229  uint32_t valid_until;
231  evtimer_msg_event_t timeout;
243 
247 extern mutex_t _nib_mutex;
248 
252 extern evtimer_msg_t _nib_evtimer;
253 
262 extern _nib_dr_entry_t *_prime_def_router;
263 
267 void _nib_init(void);
268 
276 static inline unsigned _nib_onl_get_if(const _nib_onl_entry_t *node)
277 {
278  return (node->info & _NIB_IF_MASK) >> _NIB_IF_POS;
279 }
280 
287 static inline void _nib_onl_set_if(_nib_onl_entry_t *node, unsigned iface)
288 {
289  assert(iface <= _NIB_IF_MAX);
290  node->info &= ~(_NIB_IF_MASK);
291  node->info |= ((iface << _NIB_IF_POS) & _NIB_IF_MASK);
292 }
293 
305 _nib_onl_entry_t *_nib_onl_alloc(const ipv6_addr_t *addr, unsigned iface);
306 
315 static inline bool _nib_onl_clear(_nib_onl_entry_t *node)
316 {
317  if (node->mode == _EMPTY) {
318  memset(node, 0, sizeof(_nib_onl_entry_t));
319  return true;
320  }
321  return false;
322 }
323 
331 _nib_onl_entry_t *_nib_onl_iter(const _nib_onl_entry_t *last);
332 
344 _nib_onl_entry_t *_nib_onl_get(const ipv6_addr_t *addr, unsigned iface);
345 
365 _nib_onl_entry_t *_nib_nc_add(const ipv6_addr_t *addr, unsigned iface,
366  uint16_t cstate);
367 
373 void _nib_nc_remove(_nib_onl_entry_t *node);
374 
383 void _nib_nc_get(const _nib_onl_entry_t *node, gnrc_ipv6_nib_nc_t *nce);
384 
392 void _nib_nc_set_reachable(_nib_onl_entry_t *node);
393 
404 static inline _nib_onl_entry_t *_nib_dad_add(const ipv6_addr_t *addr)
405 {
406  assert(addr != NULL);
407  _nib_onl_entry_t *node = _nib_onl_alloc(addr, 0);
408 
409  if (node != NULL) {
410  node->mode |= (_DAD);
411  }
412  return node;
413 }
414 
420 static inline void _nib_dad_remove(_nib_onl_entry_t *node)
421 {
422  node->mode &= ~(_DAD);
423  _nib_onl_clear(node);
424 }
425 
439 _nib_dr_entry_t *_nib_drl_add(const ipv6_addr_t *addr, unsigned iface);
440 
448 void _nib_drl_remove(_nib_dr_entry_t *nib_dr);
449 
457 _nib_dr_entry_t *_nib_drl_iter(const _nib_dr_entry_t *last);
458 
471 _nib_dr_entry_t *_nib_drl_get(const ipv6_addr_t *router_addr, unsigned iface);
472 
482 void _nib_drl_ft_get(const _nib_dr_entry_t *drl, gnrc_ipv6_nib_ft_t *fte);
483 
492 _nib_dr_entry_t *_nib_drl_get_dr(void);
493 
511 _nib_offl_entry_t *_nib_offl_alloc(const ipv6_addr_t *next_hop, unsigned iface,
512  const ipv6_addr_t *pfx, unsigned pfx_len);
513 
519 void _nib_offl_clear(_nib_offl_entry_t *dst);
520 
528 _nib_offl_entry_t *_nib_offl_iter(const _nib_offl_entry_t *last);
529 
538 bool _nib_offl_is_entry(const _nib_offl_entry_t *entry);
539 
558 static inline _nib_offl_entry_t *_nib_offl_add(const ipv6_addr_t *next_hop,
559  unsigned iface,
560  const ipv6_addr_t *pfx,
561  unsigned pfx_len, uint8_t mode)
562 {
563  _nib_offl_entry_t *nib_offl = _nib_offl_alloc(next_hop, iface, pfx, pfx_len);
564 
565  if (nib_offl != NULL) {
566  nib_offl->mode |= mode;
567  }
568  return nib_offl;
569 }
570 
576 static inline void _nib_offl_remove(_nib_offl_entry_t *nib_offl, uint8_t mode)
577 {
578  nib_offl->mode &= ~mode;
579  _nib_offl_clear(nib_offl);
580 }
581 
582 #if GNRC_IPV6_NIB_CONF_DC || DOXYGEN
583 
599 static inline _nib_offl_entry_t *_nib_dc_add(const ipv6_addr_t *next_hop,
600  unsigned iface,
601  const ipv6_addr_t *dst)
602 {
603  assert((next_hop != NULL) && (dst != NULL));
604  return _nib_offl_add(next_hop, iface, dst, IPV6_ADDR_BIT_LEN, _DC);
605 }
606 
616 static inline void _nib_dc_remove(_nib_offl_entry_t *nib_offl)
617 {
618  _nib_offl_remove(nib_offl, _DC);
619 }
620 #endif /* GNRC_IPV6_NIB_CONF_DC */
621 
643 _nib_offl_entry_t *_nib_pl_add(unsigned iface,
644  const ipv6_addr_t *pfx,
645  unsigned pfx_len,
646  uint32_t valid_ltime,
647  uint32_t pref_ltime);
648 
656 void _nib_pl_remove(_nib_offl_entry_t *nib_offl);
657 
658 #if GNRC_IPV6_NIB_CONF_ROUTER || DOXYGEN
659 
679 static inline _nib_offl_entry_t *_nib_ft_add(const ipv6_addr_t *next_hop,
680  unsigned iface,
681  const ipv6_addr_t *pfx,
682  unsigned pfx_len)
683 {
684  return _nib_offl_add(next_hop, iface, pfx, pfx_len, _FT);
685 }
686 
696 static inline void _nib_ft_remove(_nib_offl_entry_t *nib_offl)
697 {
698  _nib_offl_remove(nib_offl, _FT);
699 }
700 #endif /* GNRC_IPV6_NIB_CONF_ROUTER */
701 
702 #if GNRC_IPV6_NIB_CONF_MULTIHOP_P6C || defined(DOXYGEN)
703 
713 _nib_abr_entry_t *_nib_abr_add(const ipv6_addr_t *addr);
714 
722 void _nib_abr_remove(const ipv6_addr_t *addr);
723 
733 void _nib_abr_add_pfx(_nib_abr_entry_t *abr, const _nib_offl_entry_t *offl);
734 
747 _nib_offl_entry_t *_nib_abr_iter_pfx(const _nib_abr_entry_t *abr,
748  const _nib_offl_entry_t *last);
749 
758 _nib_abr_entry_t *_nib_abr_iter(const _nib_abr_entry_t *last);
759 #endif
760 
770 void _nib_ft_get(const _nib_offl_entry_t *dst, gnrc_ipv6_nib_ft_t *fte);
771 
786 int _nib_get_route(const ipv6_addr_t *dst, gnrc_pktsnip_t *ctx,
787  gnrc_ipv6_nib_ft_t *entry);
788 
798 uint32_t _evtimer_lookup(const void *ctx, uint16_t type);
799 
808 static inline void _evtimer_add(void *ctx, int16_t type,
809  evtimer_msg_event_t *event, uint32_t offset)
810 {
811 #ifdef MODULE_GNRC_IPV6
812  kernel_pid_t target_pid = gnrc_ipv6_pid;
813 #else
814  kernel_pid_t target_pid = KERNEL_PID_LAST; /* just for testing */
815 #endif
816  evtimer_del((evtimer_t *)(&_nib_evtimer), (evtimer_event_t *)event);
817  event->event.next = NULL;
818  event->event.offset = offset;
819  event->msg.type = type;
820  event->msg.content.ptr = ctx;
821  evtimer_add_msg(&_nib_evtimer, event, target_pid);
822 }
823 
824 #ifdef __cplusplus
825 }
826 #endif
827 
828 #endif /* PRIV_NIB_INTERNAL_H */
829 
Types used by the kernel.
EUI-64 data type definition.
#define GNRC_IPV6_NIB_L2ADDR_MAX_LEN
Maximum link-layer address length (aligned)
Definitions for IPv6 addresses.
Context buffer definitions.
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
Forwarding table definitions.
bitfields operations on bitfields of arbitrary length
Forwarding table entry view on NIB.
Definition: ft.h:35
void evtimer_del(evtimer_t *evtimer, evtimer_event_t *event)
Removes an event from an event timer.
#define KERNEL_PID_LAST
The last valid PID (inclusive).
Definition: kernel_types.h:73
evtimer_msg_event_t reply_rs
Event for GNRC_IPV6_NIB_REPLY_RS.
Neighbor cache entry view on NIB.
Definition: nc.h:142
event structure
Definition: event.h:127
uint16_t info
Information flags.
uint8_t ns_sent
Neighbor solicitations sent for probing.
ipv6_addr_t ipv6
Neighbors IPv6 address.
uint8_t mode
NIB entry mode.
Neighbor cache definitions.
On-link NIB entry.
Definition: _nib-internal.h:93
struct _nib_onl_entry * next
next removable entry
Definition: _nib-internal.h:94
POSIX.1-2008 compliant version of the assert macro.
uint8_t l2addr[GNRC_IPV6_NIB_L2ADDR_MAX_LEN]
Link-layer address of _nib_onl_entry_t::next_hop.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
Event timer.
Definition: evtimer.h:70
evtimer_msg_event_t addr_reg_timeout
Event for GNRC_IPV6_NIB_ADDR_REG_TIMEOUT.
eui64_t eui64
The neighbors EUI-64 (used for DAD)
Configuration macro definitions for neighbor information base.
Definitions for GNRC&#39;s IPv6 implementation.
IPC-based evtimer definitions.
data type for packet queue nodes
Definition: pktqueue.h:37
evtimer_msg_event_t nud_timeout
Event for GNRC_IPV6_NIB_SND_UC_NS, GNRC_IPV6_NIB_SND_MC_NS, GNRC_IPV6_NIB_REACH_TIMEOUT and GNRC_IPV6...
#define BITFIELD(NAME, SIZE)
Declare a bitfield of a given size.
Definition: bitfield.h:42
#define IPV6_ADDR_BIT_LEN
Length of an IPv6 address in bit.
Definition: ipv6/addr.h:43
#define GNRC_SIXLOWPAN_CTX_SIZE
maximum number of entries in context buffer
Definition: ctx.h:38
gnrc_pktqueue_t * pktqueue
queue for packets currently in address resolution
RIOT synchronization API.
#define GNRC_IPV6_NIB_OFFL_NUMOF
Number of off-link entries in NIB.
evtimer_msg_event_t snd_na
Event for GNRC_IPV6_NIB_SND_NA.
Internal NIB-representation of the authoritative border router for multihop prefix and 6LoWPAN contex...
IPC-message event.
Definition: evtimer_msg.h:40
Type to represent parts (either headers or payload) of a packet, called snips.
Data type to represent an IPv6 address.
Definition: ipv6/addr.h:74
Mutex structure.
Definition: mutex.h:36
Data type to represent an EUI-64.
Definition: eui64.h:37
Packet queue definitions.
IPv6 neighbor discovery message type definitions.
Common interface to the software PRNG.
Default route NIB entry.
uint8_t l2addr_len
length in bytes of _nib_onl_entry_t::l2addr
Off-link NIB entry.
static void evtimer_add_msg(evtimer_msg_t *evtimer, evtimer_msg_event_t *event, kernel_pid_t target_pid)
Adds event to an event timer that handles events via IPC.
Definition: evtimer_msg.h:53
kernel_pid_t gnrc_ipv6_pid
The PID to the IPv6 thread.
Generic event.
Definition: evtimer.h:57
uint8_t mode
mode of the off-link entry