netif.h
Go to the documentation of this file.
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 
24 #ifndef NET_GNRC_NETIF_H
25 #define NET_GNRC_NETIF_H
26 
27 #include <stddef.h>
28 #include <stdint.h>
29 #include <stdbool.h>
30 
31 #include "kernel_types.h"
32 #include "msg.h"
33 #include "net/ipv6/addr.h"
34 #include "net/gnrc/netapi.h"
35 #include "net/gnrc/pkt.h"
36 #include "net/gnrc/netif/conf.h"
37 #ifdef MODULE_GNRC_LORAWAN
38 #include "net/gnrc/netif/lorawan.h"
39 #endif
40 #ifdef MODULE_GNRC_SIXLOWPAN
41 #include "net/gnrc/netif/6lo.h"
42 #endif
43 #if defined(MODULE_GNRC_NETIF_DEDUP) && (GNRC_NETIF_L2ADDR_MAXLEN > 0)
44 #include "net/gnrc/netif/dedup.h"
45 #endif
46 #include "net/gnrc/netif/flags.h"
47 #ifdef MODULE_GNRC_IPV6
48 #include "net/gnrc/netif/ipv6.h"
49 #endif
50 #ifdef MODULE_GNRC_MAC
51 #include "net/gnrc/netif/mac.h"
52 #endif
53 #include "net/ndp.h"
54 #include "net/netdev.h"
55 #include "net/netopt.h"
56 #ifdef MODULE_NETSTATS_L2
57 #include "net/netstats.h"
58 #endif
59 #include "rmutex.h"
60 #include "net/netif.h"
61 
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
70 
74 typedef struct {
79 #ifdef MODULE_NETSTATS_L2
80  netstats_t stats;
81 #endif
82 #if defined(MODULE_GNRC_LORAWAN) || DOXYGEN
84 #endif
85 #if defined(MODULE_GNRC_IPV6) || DOXYGEN
87 #endif
88 #if defined(MODULE_GNRC_MAC) || DOXYGEN
90 #endif /* MODULE_GNRC_MAC */
91 
96  uint32_t flags;
97 #if (GNRC_NETIF_L2ADDR_MAXLEN > 0) || DOXYGEN
98 
104  uint8_t l2addr[GNRC_NETIF_L2ADDR_MAXLEN];
105 
111  uint8_t l2addr_len;
112 #if defined(MODULE_GNRC_NETIF_DEDUP) || DOXYGEN
113 
119 #endif
120 #endif
121 #if defined(MODULE_GNRC_SIXLOWPAN) || DOXYGEN
123 #endif
124  uint8_t cur_hl;
125  uint8_t device_type;
127 } gnrc_netif_t;
128 
148  void (*init)(gnrc_netif_t *netif);
149 
170  int (*send)(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt);
171 
188  gnrc_pktsnip_t *(*recv)(gnrc_netif_t *netif);
189 
204  int (*get)(gnrc_netif_t *netif, gnrc_netapi_opt_t *opt);
205 
221  int (*set)(gnrc_netif_t *netif, const gnrc_netapi_opt_t *opt);
222 
233  void (*msg_handler)(gnrc_netif_t *netif, msg_t *msg);
234 };
235 
244 void gnrc_netif_init_devs(void);
245 
263 int gnrc_netif_create(gnrc_netif_t *netif, char *stack, int stacksize,
264  char priority, const char *name, netdev_t *dev,
265  const gnrc_netif_ops_t *ops);
266 
272 unsigned gnrc_netif_numof(void);
273 
285 static inline bool gnrc_netif_highlander(void)
286 {
288 }
289 
299 
309 
330 static inline int gnrc_netif_ipv6_addrs_get(const gnrc_netif_t *netif,
331  ipv6_addr_t *addrs,
332  size_t max_len)
333 {
334  assert(netif != NULL);
335  assert(addrs != NULL);
336  assert(max_len >= sizeof(ipv6_addr_t));
337  return gnrc_netapi_get(netif->pid, NETOPT_IPV6_ADDR, 0, addrs, max_len);
338 }
339 
361 static inline int gnrc_netif_ipv6_addr_add(const gnrc_netif_t *netif,
362  ipv6_addr_t *addr, unsigned pfx_len,
363  uint8_t flags)
364 {
365  assert(netif != NULL);
366  assert(addr != NULL);
367  assert((pfx_len > 0) && (pfx_len <= 128));
368  return gnrc_netapi_set(netif->pid, NETOPT_IPV6_ADDR,
369  ((pfx_len << 8U) | flags), addr,
370  sizeof(ipv6_addr_t));
371 }
372 
386 static inline int gnrc_netif_ipv6_addr_remove(const gnrc_netif_t *netif,
387  ipv6_addr_t *addr)
388 {
389  assert(netif != NULL);
390  assert(addr != NULL);
392  0, addr, sizeof(ipv6_addr_t));
393 }
394 
415 static inline int gnrc_netif_ipv6_groups_get(const gnrc_netif_t *netif,
416  ipv6_addr_t *groups,
417  size_t max_len)
418 {
419  assert(netif != NULL);
420  assert(groups != NULL);
421  assert(max_len >= sizeof(ipv6_addr_t));
422  return gnrc_netapi_get(netif->pid, NETOPT_IPV6_GROUP, 0, groups, max_len);
423 }
424 
439 static inline int gnrc_netif_ipv6_group_join(const gnrc_netif_t *netif,
440  ipv6_addr_t *group)
441 {
442  assert(netif != NULL);
443  assert(group != NULL);
444  return gnrc_netapi_set(netif->pid, NETOPT_IPV6_GROUP, 0, group,
445  sizeof(ipv6_addr_t));
446 }
447 
461 static inline int gnrc_netif_ipv6_group_leave(const gnrc_netif_t *netif,
462  ipv6_addr_t *group)
463 {
464  assert(netif != NULL);
465  assert(group != NULL);
466  return gnrc_netapi_set(netif->pid, NETOPT_IPV6_GROUP_LEAVE, 0, group,
467  sizeof(ipv6_addr_t));
468 }
469 
478 
491 
504  const gnrc_netapi_opt_t *opt);
505 
522 char *gnrc_netif_addr_to_str(const uint8_t *addr, size_t addr_len, char *out);
523 
542 size_t gnrc_netif_addr_from_str(const char *str, uint8_t *out);
543 
553 static inline int gnrc_netif_send(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
554 {
555  return gnrc_netapi_send(netif->pid, pkt);
556 }
557 
558 #ifdef __cplusplus
559 }
560 #endif
561 
562 #endif /* NET_GNRC_NETIF_H */
563 
static int gnrc_netif_ipv6_group_leave(const gnrc_netif_t *netif, ipv6_addr_t *group)
Leaves an IPv6 multicast group on an interface (if IPv6 is supported)
Definition: netif.h:461
Types used by the kernel.
Definition of net statistics.
uint8_t cur_hl
Current hop-limit for out-going packets.
Definition: netif.h:124
void(* msg_handler)(gnrc_netif_t *netif, msg_t *msg)
Message handler for network interface.
Definition: netif.h:233
static int gnrc_netapi_send(kernel_pid_t pid, gnrc_pktsnip_t *pkt)
Shortcut function for sending GNRC_NETAPI_MSG_TYPE_SND messages.
Definition: netapi.h:154
6Lo component of gnrc_netif_t
Definition: 6lo.h:30
Definitions for IPv6 addresses.
size_t gnrc_netif_addr_from_str(const char *str, uint8_t *out)
Parses a string of colon-separated hexadecimals to a hardware address.
uint32_t flags
Flags for the interface.
Definition: netif.h:96
int(* send)(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
Send a packet over the network interface.
Definition: netif.h:170
General definitions for network packets and their helper functions.
int gnrc_netif_set_from_netdev(gnrc_netif_t *netif, const gnrc_netapi_opt_t *opt)
Default operation for gnrc_netif_ops_t::set()
netif_t netif
network interface descriptor
Definition: netif.h:75
Data structure to be send for setting (GNRC_NETAPI_MSG_TYPE_SET) and getting (GNRC_NETAPI_MSG_TYPE_GE...
Definition: netapi.h:100
6LoWPAN definitions for Network interface API
Network interface descriptor.
Definition: netif.h:57
Configuration macros for Network interface API.
static int gnrc_netapi_set(kernel_pid_t pid, netopt_t opt, uint16_t context, const void *data, size_t data_len)
Shortcut function for sending GNRC_NETAPI_MSG_TYPE_SET messages and parsing the returned GNRC_NETAPI_...
Definition: netapi.h:255
Definitions low-level network driver interface.
Common MAC module definitions for Network interface API
Generic interface to communicate with GNRC modules.
const gnrc_netif_ops_t * ops
Operations of the network interface.
Definition: netif.h:76
Mutex structure.
Definition: rmutex.h:43
IPv6 definitions for Network interface API.
Global statistics struct.
Definition: netstats.h:43
uint8_t device_type
Device type.
Definition: netif.h:125
static int gnrc_netif_ipv6_addr_add(const gnrc_netif_t *netif, ipv6_addr_t *addr, unsigned pfx_len, uint8_t flags)
Adds an (unicast or anycast) IPv6 address to an interface (if IPv6 is supported)
Definition: netif.h:361
Common MAC module component of gnrc_netif_mac_t
Definition: mac.h:72
static int gnrc_netif_send(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
Send a GNRC packet via a given gnrc_netif_t interface.
Definition: netif.h:553
int gnrc_netif_get_from_netdev(gnrc_netif_t *netif, gnrc_netapi_opt_t *opt)
Default operation for gnrc_netif_ops_t::get()
gnrc_netif_6lo_t sixlo
6Lo component
Definition: netif.h:122
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
static int gnrc_netif_ipv6_addr_remove(const gnrc_netif_t *netif, ipv6_addr_t *addr)
Removes a (unicast or anycast) IPv6 address from an interface (if IPv6 is supported) ...
Definition: netif.h:386
uint8_t l2addr_len
Length in bytes of gnrc_netif_t::l2addr.
Definition: netif.h:111
netdev_t * dev
Network device of the network interface.
Definition: netif.h:77
IPv6 component for gnrc_netif_t.
Definition: ipv6.h:78
#define IS_ACTIVE(macro)
Allows to verify a macro definition outside the preprocessor.
void gnrc_netif_init_devs(void)
Initialize all available network interfaces.
static int gnrc_netapi_get(kernel_pid_t pid, netopt_t opt, uint16_t context, void *data, size_t max_len)
Shortcut function for sending GNRC_NETAPI_MSG_TYPE_GET messages and parsing the returned GNRC_NETAPI_...
Definition: netapi.h:233
Common network interface API definitions.
static bool gnrc_netif_highlander(void)
Check if there can only be one gnrc_netif_t interface.
Definition: netif.h:285
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
int gnrc_netif_create(gnrc_netif_t *netif, char *stack, int stacksize, char priority, const char *name, netdev_t *dev, const gnrc_netif_ops_t *ops)
Creates a network interface.
Messaging API for inter process communication.
gnrc_netif_ipv6_t ipv6
IPv6 component.
Definition: netif.h:86
void(* init)(gnrc_netif_t *netif)
Initializes network interface beyond the default settings.
Definition: netif.h:148
rmutex_t mutex
Mutex of the interface.
Definition: netif.h:78
Structure to hold driver state.
Definition: netdev.h:283
Describes a message object which can be sent between threads.
Definition: msg.h:184
Recursive Mutex for thread synchronization.
(ipv6_addr_t) Removes an IPv6 address from an interface
Definition: netopt.h:154
void gnrc_netif_default_init(gnrc_netif_t *netif)
Default operation for gnrc_netif_ops_t::init()
(ipv6_addr_t[]) get IPv6 addresses of an interface as array of ipv6_addr_t or add an IPv6 address as ...
Definition: netopt.h:150
unsigned gnrc_netif_numof(void)
Get number of network interfaces actually allocated.
(ipv6_addr_t) Leave an IPv6 multicast group on an interface
Definition: netopt.h:182
static int gnrc_netif_ipv6_addrs_get(const gnrc_netif_t *netif, ipv6_addr_t *addrs, size_t max_len)
Gets the (unicast on anycast) IPv6 address of an interface (if IPv6 is supported) ...
Definition: netif.h:330
kernel_pid_t pid
PID of the network interface&#39;s thread.
Definition: netif.h:126
GNRC LoRaWAN interface descriptor.
Definition: lorawan.h:35
#define GNRC_NETIF_L2ADDR_MAXLEN
Maximum length of the link-layer address.
Definition: conf.h:134
Representation of a network interface.
Definition: netif.h:74
Definition of global configuration options.
static int gnrc_netif_ipv6_group_join(const gnrc_netif_t *netif, ipv6_addr_t *group)
Joins an IPv6 multicast group on an interface (if IPv6 is supported)
Definition: netif.h:439
gnrc_netif_t * gnrc_netif_get_by_pid(kernel_pid_t pid)
Get network interface by PID.
char * gnrc_netif_addr_to_str(const uint8_t *addr, size_t addr_len, char *out)
Converts a hardware address to a human readable string.
LoRaWAN adaption for Network interface API.
gnrc_netif_mac_t mac
Common MAC module component
Definition: netif.h:89
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
gnrc_netif_lorawan_t lorawan
LoRaWAN component.
Definition: netif.h:83
Flag definitions for Network interface API.
IPv6 neighbor discovery message type definitions.
#define GNRC_NETIF_SINGLE
Single interface optimizations.
Definition: conf.h:41
gnrc_netif_dedup_t last_pkt
Last received packet information.
Definition: netif.h:118
Structure to store information on the last broadcast packet received.
Definition: dedup.h:41
static int gnrc_netif_ipv6_groups_get(const gnrc_netif_t *netif, ipv6_addr_t *groups, size_t max_len)
Gets the IPv6 multicast groups an interface is joined to (if IPv6 is supported)
Definition: netif.h:415
(ipv6_addr_t) get IPv6 multicast groups of an interface as array of ipv6_addr_t or join an IPv6 multi...
Definition: netopt.h:178
gnrc_netif_t * gnrc_netif_iter(const gnrc_netif_t *prev)
Iterate over all network interfaces.