_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 evtimer_msg_t _nib_evtimer;
248 
257 extern _nib_dr_entry_t *_prime_def_router;
258 
262 void _nib_init(void);
263 
267 void _nib_acquire(void);
268 
272 void _nib_release(void);
273 
281 static inline unsigned _nib_onl_get_if(const _nib_onl_entry_t *node)
282 {
283  return (node->info & _NIB_IF_MASK) >> _NIB_IF_POS;
284 }
285 
292 static inline void _nib_onl_set_if(_nib_onl_entry_t *node, unsigned iface)
293 {
294  assert(iface <= _NIB_IF_MAX);
295  node->info &= ~(_NIB_IF_MASK);
296  node->info |= ((iface << _NIB_IF_POS) & _NIB_IF_MASK);
297 }
298 
310 _nib_onl_entry_t *_nib_onl_alloc(const ipv6_addr_t *addr, unsigned iface);
311 
320 static inline bool _nib_onl_clear(_nib_onl_entry_t *node)
321 {
322  if (node->mode == _EMPTY) {
323  memset(node, 0, sizeof(_nib_onl_entry_t));
324  return true;
325  }
326  return false;
327 }
328 
336 _nib_onl_entry_t *_nib_onl_iter(const _nib_onl_entry_t *last);
337 
349 _nib_onl_entry_t *_nib_onl_get(const ipv6_addr_t *addr, unsigned iface);
350 
370 _nib_onl_entry_t *_nib_nc_add(const ipv6_addr_t *addr, unsigned iface,
371  uint16_t cstate);
372 
378 void _nib_nc_remove(_nib_onl_entry_t *node);
379 
388 void _nib_nc_get(const _nib_onl_entry_t *node, gnrc_ipv6_nib_nc_t *nce);
389 
397 void _nib_nc_set_reachable(_nib_onl_entry_t *node);
398 
409 static inline _nib_onl_entry_t *_nib_dad_add(const ipv6_addr_t *addr)
410 {
411  assert(addr != NULL);
412  _nib_onl_entry_t *node = _nib_onl_alloc(addr, 0);
413 
414  if (node != NULL) {
415  node->mode |= (_DAD);
416  }
417  return node;
418 }
419 
425 static inline void _nib_dad_remove(_nib_onl_entry_t *node)
426 {
427  node->mode &= ~(_DAD);
428  _nib_onl_clear(node);
429 }
430 
444 _nib_dr_entry_t *_nib_drl_add(const ipv6_addr_t *addr, unsigned iface);
445 
453 void _nib_drl_remove(_nib_dr_entry_t *nib_dr);
454 
462 _nib_dr_entry_t *_nib_drl_iter(const _nib_dr_entry_t *last);
463 
476 _nib_dr_entry_t *_nib_drl_get(const ipv6_addr_t *router_addr, unsigned iface);
477 
487 void _nib_drl_ft_get(const _nib_dr_entry_t *drl, gnrc_ipv6_nib_ft_t *fte);
488 
497 _nib_dr_entry_t *_nib_drl_get_dr(void);
498 
516 _nib_offl_entry_t *_nib_offl_alloc(const ipv6_addr_t *next_hop, unsigned iface,
517  const ipv6_addr_t *pfx, unsigned pfx_len);
518 
524 void _nib_offl_clear(_nib_offl_entry_t *dst);
525 
533 _nib_offl_entry_t *_nib_offl_iter(const _nib_offl_entry_t *last);
534 
543 bool _nib_offl_is_entry(const _nib_offl_entry_t *entry);
544 
563 static inline _nib_offl_entry_t *_nib_offl_add(const ipv6_addr_t *next_hop,
564  unsigned iface,
565  const ipv6_addr_t *pfx,
566  unsigned pfx_len, uint8_t mode)
567 {
568  _nib_offl_entry_t *nib_offl = _nib_offl_alloc(next_hop, iface, pfx, pfx_len);
569 
570  if (nib_offl != NULL) {
571  nib_offl->mode |= mode;
572  }
573  return nib_offl;
574 }
575 
581 static inline void _nib_offl_remove(_nib_offl_entry_t *nib_offl, uint8_t mode)
582 {
583  nib_offl->mode &= ~mode;
584  _nib_offl_clear(nib_offl);
585 }
586 
587 #if GNRC_IPV6_NIB_CONF_DC || DOXYGEN
588 
604 static inline _nib_offl_entry_t *_nib_dc_add(const ipv6_addr_t *next_hop,
605  unsigned iface,
606  const ipv6_addr_t *dst)
607 {
608  assert((next_hop != NULL) && (dst != NULL));
609  return _nib_offl_add(next_hop, iface, dst, IPV6_ADDR_BIT_LEN, _DC);
610 }
611 
621 static inline void _nib_dc_remove(_nib_offl_entry_t *nib_offl)
622 {
623  _nib_offl_remove(nib_offl, _DC);
624 }
625 #endif /* GNRC_IPV6_NIB_CONF_DC */
626 
648 _nib_offl_entry_t *_nib_pl_add(unsigned iface,
649  const ipv6_addr_t *pfx,
650  unsigned pfx_len,
651  uint32_t valid_ltime,
652  uint32_t pref_ltime);
653 
661 void _nib_pl_remove(_nib_offl_entry_t *nib_offl);
662 
663 #if GNRC_IPV6_NIB_CONF_ROUTER || DOXYGEN
664 
684 static inline _nib_offl_entry_t *_nib_ft_add(const ipv6_addr_t *next_hop,
685  unsigned iface,
686  const ipv6_addr_t *pfx,
687  unsigned pfx_len)
688 {
689  return _nib_offl_add(next_hop, iface, pfx, pfx_len, _FT);
690 }
691 
701 static inline void _nib_ft_remove(_nib_offl_entry_t *nib_offl)
702 {
703  _nib_offl_remove(nib_offl, _FT);
704 }
705 #endif /* GNRC_IPV6_NIB_CONF_ROUTER */
706 
707 #if GNRC_IPV6_NIB_CONF_MULTIHOP_P6C || defined(DOXYGEN)
708 
718 _nib_abr_entry_t *_nib_abr_add(const ipv6_addr_t *addr);
719 
727 void _nib_abr_remove(const ipv6_addr_t *addr);
728 
738 void _nib_abr_add_pfx(_nib_abr_entry_t *abr, const _nib_offl_entry_t *offl);
739 
752 _nib_offl_entry_t *_nib_abr_iter_pfx(const _nib_abr_entry_t *abr,
753  const _nib_offl_entry_t *last);
754 
763 _nib_abr_entry_t *_nib_abr_iter(const _nib_abr_entry_t *last);
764 #endif
765 
775 void _nib_ft_get(const _nib_offl_entry_t *dst, gnrc_ipv6_nib_ft_t *fte);
776 
791 int _nib_get_route(const ipv6_addr_t *dst, gnrc_pktsnip_t *ctx,
792  gnrc_ipv6_nib_ft_t *entry);
793 
803 uint32_t _evtimer_lookup(const void *ctx, uint16_t type);
804 
813 static inline void _evtimer_add(void *ctx, int16_t type,
814  evtimer_msg_event_t *event, uint32_t offset)
815 {
816 #ifdef MODULE_GNRC_IPV6
817  kernel_pid_t target_pid = gnrc_ipv6_pid;
818 #else
819  kernel_pid_t target_pid = KERNEL_PID_LAST; /* just for testing */
820 #endif
821  evtimer_del((evtimer_t *)(&_nib_evtimer), (evtimer_event_t *)event);
822  event->event.next = NULL;
823  event->event.offset = offset;
824  event->msg.type = type;
825  event->msg.content.ptr = ctx;
826  evtimer_add_msg(&_nib_evtimer, event, target_pid);
827 }
828 
829 #ifdef __cplusplus
830 }
831 #endif
832 
833 #endif /* PRIV_NIB_INTERNAL_H */
834 
Types used by the kernel.
EUI-64 data type definition.
#define GNRC_IPV6_NIB_L2ADDR_MAX_LEN
Maximum link-layer address length (aligned)
Definition: conf.h:236
Definitions for IPv6 addresses.
Context buffer definitions.
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.
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:139
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.
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
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.
Event timer.
Definition: evtimer.h:70
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
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:46
#define IPV6_ADDR_BIT_LEN
Length of an IPv6 address in bit.
Definition: 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
Mutex for thread synchronization.
#define GNRC_IPV6_NIB_OFFL_NUMOF
Number of off-link entries in NIB.
Definition: conf.h:266
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.
Definition: pkt.h:107
Data type to represent an IPv6 address.
Definition: addr.h:74
Data type to represent an EUI-64.
Definition: eui64.h:55
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
#define KERNEL_PID_LAST
The last valid PID (inclusive).
Definition: kernel_types.h:73
uint8_t mode
mode of the off-link entry