netif.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017-20 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 
29 #ifndef NET_GNRC_NETIF_H
30 #define NET_GNRC_NETIF_H
31 
32 #include <stddef.h>
33 #include <stdint.h>
34 #include <stdbool.h>
35 
36 #include "sched.h"
37 #include "msg.h"
38 #ifdef MODULE_GNRC_NETIF_BUS
39 #include "msg_bus.h"
40 #endif
41 #include "event.h"
42 #include "net/ipv6/addr.h"
43 #include "net/gnrc/netapi.h"
44 #include "net/gnrc/pkt.h"
45 #include "net/gnrc/netif/conf.h"
46 #if IS_USED(MODULE_GNRC_NETIF_LORAWAN)
47 #include "net/gnrc/netif/lorawan.h"
48 #endif
49 #if IS_USED(MODULE_GNRC_NETIF_6LO)
50 #include "net/gnrc/netif/6lo.h"
51 #endif
52 #if defined(MODULE_GNRC_NETIF_DEDUP) && (GNRC_NETIF_L2ADDR_MAXLEN > 0)
53 #include "net/gnrc/netif/dedup.h"
54 #endif
55 #include "net/gnrc/netif/flags.h"
56 #if IS_USED(MODULE_GNRC_NETIF_IPV6)
57 #include "net/gnrc/netif/ipv6.h"
58 #endif
59 #if IS_USED(MODULE_GNRC_NETIF_MAC)
60 #include "net/gnrc/netif/mac.h"
61 #endif
62 #if IS_USED(MODULE_GNRC_NETIF_PKTQ)
64 #endif
65 #include "net/ndp.h"
66 #include "net/netdev.h"
67 #include "net/netopt.h"
68 #ifdef MODULE_NETSTATS_L2
69 #include "net/netstats.h"
70 #endif
71 #include "rmutex.h"
72 #include "net/netif.h"
73 
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77 
81 typedef enum {
82 #ifdef MODULE_GNRC_IPV6
85 #endif
86  GNRC_NETIF_BUS_NUMOF
88 
92 typedef enum {
106 
111 
115 typedef struct {
120 #ifdef MODULE_NETSTATS_L2
121  netstats_t stats;
122 #endif
123 #if IS_USED(MODULE_GNRC_NETIF_LORAWAN) || defined(DOXYGEN)
125 #endif
126 #if IS_USED(MODULE_GNRC_NETIF_IPV6) || defined(DOXYGEN)
128 #endif
129 #if IS_USED(MODULE_GNRC_NETIF_MAC) || defined(DOXYGEN)
131 #endif /* IS_USED(MODULE_GNRC_NETIF_MAC) || defined(DOXYGEN) */
132 #if IS_USED(MODULE_GNRC_NETIF_BUS) || DOXYGEN
133  msg_bus_t bus[GNRC_NETIF_BUS_NUMOF];
134 #endif
135 
140  uint32_t flags;
141 #if IS_USED(MODULE_GNRC_NETIF_EVENTS) || defined(DOXYGEN)
142 
150 #endif /* MODULE_GNRC_NETIF_EVENTS */
151 #if (GNRC_NETIF_L2ADDR_MAXLEN > 0) || DOXYGEN
152 
158  uint8_t l2addr[GNRC_NETIF_L2ADDR_MAXLEN];
159 
165  uint8_t l2addr_len;
166 #if defined(MODULE_GNRC_NETIF_DEDUP) || DOXYGEN
167 
173 #endif
174 #endif
175 #if IS_USED(MODULE_GNRC_NETIF_6LO) || defined(DOXYGEN)
177 #endif
178 #if IS_USED(MODULE_GNRC_NETIF_PKTQ) || defined(DOXYGEN)
179 
185 #endif
186  uint8_t cur_hl;
187  uint8_t device_type;
189 } gnrc_netif_t;
190 
210  void (*init)(gnrc_netif_t *netif);
211 
232  int (*send)(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt);
233 
250  gnrc_pktsnip_t *(*recv)(gnrc_netif_t *netif);
251 
266  int (*get)(gnrc_netif_t *netif, gnrc_netapi_opt_t *opt);
267 
283  int (*set)(gnrc_netif_t *netif, const gnrc_netapi_opt_t *opt);
284 
295  void (*msg_handler)(gnrc_netif_t *netif, msg_t *msg);
296 };
297 
306 void gnrc_netif_init_devs(void);
307 
325 int gnrc_netif_create(gnrc_netif_t *netif, char *stack, int stacksize,
326  char priority, const char *name, netdev_t *dev,
327  const gnrc_netif_ops_t *ops);
328 
334 unsigned gnrc_netif_numof(void);
335 
347 static inline bool gnrc_netif_highlander(void)
348 {
349  return IS_USED(MODULE_GNRC_NETIF_SINGLE);
350 }
351 
361 
371 
392 static inline int gnrc_netif_ipv6_addrs_get(const gnrc_netif_t *netif,
393  ipv6_addr_t *addrs,
394  size_t max_len)
395 {
396  assert(netif != NULL);
397  assert(addrs != NULL);
398  assert(max_len >= sizeof(ipv6_addr_t));
399  return gnrc_netapi_get(netif->pid, NETOPT_IPV6_ADDR, 0, addrs, max_len);
400 }
401 
423 static inline int gnrc_netif_ipv6_addr_add(const gnrc_netif_t *netif,
424  ipv6_addr_t *addr, unsigned pfx_len,
425  uint8_t flags)
426 {
427  assert(netif != NULL);
428  assert(addr != NULL);
429  assert((pfx_len > 0) && (pfx_len <= 128));
430  return gnrc_netapi_set(netif->pid, NETOPT_IPV6_ADDR,
431  ((pfx_len << 8U) | flags), addr,
432  sizeof(ipv6_addr_t));
433 }
434 
448 static inline int gnrc_netif_ipv6_addr_remove(const gnrc_netif_t *netif,
449  ipv6_addr_t *addr)
450 {
451  assert(netif != NULL);
452  assert(addr != NULL);
454  0, addr, sizeof(ipv6_addr_t));
455 }
456 
477 static inline int gnrc_netif_ipv6_groups_get(const gnrc_netif_t *netif,
478  ipv6_addr_t *groups,
479  size_t max_len)
480 {
481  assert(netif != NULL);
482  assert(groups != NULL);
483  assert(max_len >= sizeof(ipv6_addr_t));
484  return gnrc_netapi_get(netif->pid, NETOPT_IPV6_GROUP, 0, groups, max_len);
485 }
486 
501 static inline int gnrc_netif_ipv6_group_join(const gnrc_netif_t *netif,
502  ipv6_addr_t *group)
503 {
504  assert(netif != NULL);
505  assert(group != NULL);
506  return gnrc_netapi_set(netif->pid, NETOPT_IPV6_GROUP, 0, group,
507  sizeof(ipv6_addr_t));
508 }
509 
523 static inline int gnrc_netif_ipv6_group_leave(const gnrc_netif_t *netif,
524  ipv6_addr_t *group)
525 {
526  assert(netif != NULL);
527  assert(group != NULL);
528  return gnrc_netapi_set(netif->pid, NETOPT_IPV6_GROUP_LEAVE, 0, group,
529  sizeof(ipv6_addr_t));
530 }
531 
540 
553 
566  const gnrc_netapi_opt_t *opt);
567 
586 static inline char *gnrc_netif_addr_to_str(const uint8_t *addr, size_t addr_len, char *out)
587 {
588  return netif_addr_to_str(addr, addr_len, out);
589 }
590 
611 static inline size_t gnrc_netif_addr_from_str(const char *str, uint8_t *out)
612 {
613  return netif_addr_from_str(str, out);
614 }
615 
625 static inline int gnrc_netif_send(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
626 {
627  return gnrc_netapi_send(netif->pid, pkt);
628 }
629 
630 #if defined(MODULE_GNRC_NETIF_BUS) || DOXYGEN
631 
640  gnrc_netif_bus_t type)
641 {
642  assert(type < GNRC_NETIF_BUS_NUMOF);
643  return &netif->bus[type];
644 }
645 #endif /* MODULE_GNRC_NETIF_BUS */
646 
647 #ifdef __cplusplus
648 }
649 #endif
650 
651 #endif /* NET_GNRC_NETIF_H */
652 
netapi.h
Generic interface to communicate with GNRC modules.
gnrc_ipv6_event_t
gnrc_ipv6_event_t
Event types for GNRC_NETIF_BUS_IPV6 per-interface message bus.
Definition: netif.h:92
kernel_pid_t
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:125
netif_t
Network interface descriptor.
Definition: netif.h:63
gnrc_netapi_get
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
netstats_t
Global statistics struct.
Definition: netstats.h:43
gnrc_netif_ops::send
int(* send)(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
Send a packet over the network interface.
Definition: netif.h:232
gnrc_netif_ipv6_groups_get
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:477
gnrc_netif_t::sixlo
gnrc_netif_6lo_t sixlo
6Lo component
Definition: netif.h:176
gnrc_netif_t::lorawan
gnrc_netif_lorawan_t lorawan
LoRaWAN component.
Definition: netif.h:124
gnrc_netif_ops::set
int(* set)(gnrc_netif_t *netif, const gnrc_netapi_opt_t *opt)
Sets an option from the network interface.
Definition: netif.h:283
gnrc_netif_ipv6_group_leave
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:523
gnrc_netif_t::mac
gnrc_netif_mac_t mac
Common MAC module component
Definition: netif.h:130
msg_bus.h
Messaging Bus API for inter process message broadcast.
NETOPT_IPV6_ADDR_REMOVE
@ NETOPT_IPV6_ADDR_REMOVE
(ipv6_addr_t) Removes an IPv6 address from an interface
Definition: netopt.h:144
GNRC_IPV6_EVENT_ADDR_VALID
@ GNRC_IPV6_EVENT_ADDR_VALID
Address becomes valid.
Definition: netif.h:104
netif_addr_from_str
size_t netif_addr_from_str(const char *str, uint8_t *out)
Parses a string of colon-separated hexadecimals to a hardware address.
assert
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
ipv6_addr_t
Data type to represent an IPv6 address.
Definition: addr.h:74
gnrc_netif_t::last_pkt
gnrc_netif_dedup_t last_pkt
Last received packet information.
Definition: netif.h:172
gnrc_netapi_opt_t
Data structure to be send for setting (GNRC_NETAPI_MSG_TYPE_SET) and getting (GNRC_NETAPI_MSG_TYPE_GE...
Definition: netapi.h:100
addr.h
Definitions for IPv6 addresses.
netopt.h
Definition of global configuration options.
gnrc_netif_ipv6_addr_add
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:423
gnrc_netif_set_from_netdev
int gnrc_netif_set_from_netdev(gnrc_netif_t *netif, const gnrc_netapi_opt_t *opt)
Default operation for gnrc_netif_ops_t::set()
NETOPT_IPV6_GROUP
@ NETOPT_IPV6_GROUP
(ipv6_addr_t) get IPv6 multicast groups of an interface as array of ipv6_addr_t or join an IPv6 multi...
Definition: netopt.h:168
gnrc_netif_get_from_netdev
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_ipv6_addr_remove
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:448
event
event structure
Definition: event.h:142
gnrc_netif_t::event_isr
event_t event_isr
ISR event for the network device.
Definition: netif.h:149
gnrc_netif_get_bus
static msg_bus_t * gnrc_netif_get_bus(gnrc_netif_t *netif, gnrc_netif_bus_t type)
Get a message bus of a given gnrc_netif_t interface.
Definition: netif.h:639
gnrc_netapi_send
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
gnrc_netif_addr_to_str
static 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.
Definition: netif.h:586
event_queue_t
event queue structure
Definition: event.h:150
msg.h
Messaging API for inter process communication.
lorawan.h
LoRaWAN adaption for Network interface API.
gnrc_netif_t::cur_hl
uint8_t cur_hl
Current hop-limit for out-going packets.
Definition: netif.h:186
sched.h
Scheduler API definition.
gnrc_netapi_set
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
type.h
Send queue for Network interface API type definitions
GNRC_NETIF_L2ADDR_MAXLEN
#define GNRC_NETIF_L2ADDR_MAXLEN
Maximum length of the link-layer address.
Definition: conf.h:153
gnrc_netif_ipv6_t
IPv6 component for gnrc_netif_t.
Definition: ipv6.h:78
gnrc_netif_highlander
static bool gnrc_netif_highlander(void)
Check if there can only be one gnrc_netif_t interface.
Definition: netif.h:347
gnrc_netif_dedup_t
Structure to store information on the last broadcast packet received.
Definition: dedup.h:41
mac.h
Common MAC module definitions for Network interface API
gnrc_netif_t::flags
uint32_t flags
Flags for the interface.
Definition: netif.h:140
NETOPT_IPV6_ADDR
@ NETOPT_IPV6_ADDR
(ipv6_addr_t[]) get IPv6 addresses of an interface as array of ipv6_addr_t or add an IPv6 address as ...
Definition: netopt.h:140
netif.h
Common network interface API definitions.
gnrc_netif_create
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.
gnrc_netif_send
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:625
conf.h
Configuration macros for Network interface API.
ipv6.h
IPv6 definitions for Network interface API.
gnrc_netif_t::ipv6
gnrc_netif_ipv6_t ipv6
IPv6 component.
Definition: netif.h:127
gnrc_netif_t
Representation of a network interface.
Definition: netif.h:115
netdev.h
Definitions low-level network driver interface.
gnrc_netif_lorawan_t
GNRC LoRaWAN interface descriptor.
Definition: lorawan.h:35
gnrc_netif_addr_from_str
static size_t gnrc_netif_addr_from_str(const char *str, uint8_t *out)
Parses a string of colon-separated hexadecimals to a hardware address.
Definition: netif.h:611
pkt.h
General definitions for network packets and their helper functions.
gnrc_netif_bus_t
gnrc_netif_bus_t
Per-Interface Event Message Buses.
Definition: netif.h:81
gnrc_netif_t::pid
kernel_pid_t pid
PID of the network interface's thread.
Definition: netif.h:188
gnrc_netif_init_devs
void gnrc_netif_init_devs(void)
Initialize all available network interfaces.
ndp.h
IPv6 neighbor discovery message type definitions.
gnrc_netif_ops::get
int(* get)(gnrc_netif_t *netif, gnrc_netapi_opt_t *opt)
Gets an option from the network interface.
Definition: netif.h:266
gnrc_netif_ops::init
void(* init)(gnrc_netif_t *netif)
Initializes network interface beyond the default settings.
Definition: netif.h:210
netdev
Structure to hold driver state.
Definition: netdev.h:302
rmutex_t
Mutex structure.
Definition: rmutex.h:43
gnrc_netif_t::mutex
rmutex_t mutex
Mutex of the interface.
Definition: netif.h:119
gnrc_netif_get_by_pid
gnrc_netif_t * gnrc_netif_get_by_pid(kernel_pid_t pid)
Get network interface by PID.
rmutex.h
Recursive Mutex for thread synchronization.
msg_bus_t
A message bus is just a list of subscribers.
Definition: msg_bus.h:54
gnrc_netif_default_init
void gnrc_netif_default_init(gnrc_netif_t *netif)
Default operation for gnrc_netif_ops_t::init()
gnrc_netif_t::dev
netdev_t * dev
Network device of the network interface.
Definition: netif.h:118
gnrc_netif_6lo_t
6Lo component of gnrc_netif_t
Definition: 6lo.h:30
IS_USED
#define IS_USED(module)
Checks whether a module is being used or not. Can be used in C conditionals.
Definition: kernel_defines.h:188
NETOPT_IPV6_GROUP_LEAVE
@ NETOPT_IPV6_GROUP_LEAVE
(ipv6_addr_t) Leave an IPv6 multicast group on an interface
Definition: netopt.h:172
gnrc_netif_t::evq
event_queue_t evq
Event queue for asynchronous events.
Definition: netif.h:145
gnrc_netif_t::send_queue
gnrc_netif_pktq_t send_queue
Packet queue for sending.
Definition: netif.h:184
gnrc_netif_ipv6_group_join
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:501
gnrc_netif_t::device_type
uint8_t device_type
Device type.
Definition: netif.h:187
msg_t
Describes a message object which can be sent between threads.
Definition: msg.h:185
gnrc_netif_pktq_t
A packet queue for Network interface API with a de-queue timer.
Definition: type.h:36
GNRC_NETIF_BUS_IPV6
@ GNRC_NETIF_BUS_IPV6
provides gnrc_ipv6_event_t messages to subscribers
Definition: netif.h:83
gnrc_netif_iter
gnrc_netif_t * gnrc_netif_iter(const gnrc_netif_t *prev)
Iterate over all network interfaces.
flags.h
Flag definitions for Network interface API.
gnrc_netif_ops
Definition: netif.h:194
netstats.h
Definition of net statistics.
gnrc_netif_t::netif
netif_t netif
network interface descriptor
Definition: netif.h:116
gnrc_netif_numof
unsigned gnrc_netif_numof(void)
Get number of network interfaces actually allocated.
gnrc_netif_ops::msg_handler
void(* msg_handler)(gnrc_netif_t *netif, msg_t *msg)
Message handler for network interface.
Definition: netif.h:295
gnrc_netif_t::l2addr_len
uint8_t l2addr_len
Length in bytes of gnrc_netif_t::l2addr.
Definition: netif.h:165
gnrc_netif_ipv6_addrs_get
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:392
gnrc_netif_t::ops
const gnrc_netif_ops_t * ops
Operations of the network interface.
Definition: netif.h:117
6lo.h
6LoWPAN definitions for Network interface API
gnrc_pktsnip
Type to represent parts (either headers or payload) of a packet, called snips.
Definition: pkt.h:108
netif_addr_to_str
char * netif_addr_to_str(const uint8_t *addr, size_t addr_len, char *out)
Converts a hardware address to a human readable string.
gnrc_netif_t::bus
msg_bus_t bus[GNRC_NETIF_BUS_NUMOF]
Event Message Bus.
Definition: netif.h:133
dedup.h
gnrc_netif_mac_t
Common MAC module component of gnrc_netif_mac_t
Definition: mac.h:72