netif.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2017-2020 Freie Universität Berlin
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <stdbool.h>
32 
33 #include "sched.h"
34 #include "msg.h"
35 #ifdef MODULE_GNRC_NETIF_BUS
36 #include "msg_bus.h"
37 #endif
38 #include "event.h"
39 #include "net/ipv6/addr.h"
40 #include "net/gnrc/netapi.h"
41 #include "net/gnrc/pkt.h"
42 #include "net/gnrc/netif/conf.h"
43 #if IS_USED(MODULE_GNRC_NETIF_LORAWAN)
44 #include "net/gnrc/netif/lorawan.h"
45 #endif
46 #if IS_USED(MODULE_GNRC_NETIF_6LO)
47 #include "net/gnrc/netif/6lo.h"
48 #endif
49 #if defined(MODULE_GNRC_NETIF_DEDUP) && (GNRC_NETIF_L2ADDR_MAXLEN > 0)
50 #include "net/gnrc/netif/dedup.h"
51 #endif
52 #include "net/gnrc/netif/flags.h"
53 #if IS_USED(MODULE_GNRC_NETIF_IPV6)
54 #include "net/gnrc/netif/ipv6.h"
55 #endif
56 #if IS_USED(MODULE_GNRC_NETIF_PKTQ)
58 #endif
59 #include "net/l2util.h"
60 #include "net/ndp.h"
61 #include "net/netdev.h"
62 #include "net/netopt.h"
63 #ifdef MODULE_NETSTATS_L2
64 #include "net/netstats.h"
65 #endif
66 #include "rmutex.h"
67 #include "net/netif.h"
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
76 #define GNRC_NETIF_EVQ_INDEX_PRIO_HIGH (0)
77 
81 #if IS_USED(MODULE_BHP_EVENT)
82 #define GNRC_NETIF_EVQ_INDEX_PRIO_LOW (GNRC_NETIF_EVQ_INDEX_PRIO_HIGH + 1)
83 #else
84 #define GNRC_NETIF_EVQ_INDEX_PRIO_LOW GNRC_NETIF_EVQ_INDEX_PRIO_HIGH
85 #endif
86 
90 #define GNRC_NETIF_EVQ_NUMOF (GNRC_NETIF_EVQ_INDEX_PRIO_LOW + 1)
91 
95 typedef enum {
96 #ifdef MODULE_GNRC_IPV6
99 #endif
100  GNRC_NETIF_BUS_NUMOF
102 
106 typedef enum {
120 
124 typedef struct gnrc_netif_ops gnrc_netif_ops_t;
125 
129 typedef struct {
134 #if IS_USED(MODULE_NETSTATS_L2) || defined(DOXYGEN)
136 #endif
137 #if IS_USED(MODULE_GNRC_NETIF_LORAWAN) || defined(DOXYGEN)
139 #endif
140 #if IS_USED(MODULE_GNRC_NETIF_IPV6) || defined(DOXYGEN)
142 #endif
143 #if IS_USED(MODULE_GNRC_NETIF_BUS) || DOXYGEN
144  msg_bus_t bus[GNRC_NETIF_BUS_NUMOF];
145 #endif
151  uint32_t flags;
160 #if IS_USED(MODULE_NETDEV_NEW_API) || defined(DOXYGEN)
175 #endif
176 #if (GNRC_NETIF_L2ADDR_MAXLEN > 0) || DOXYGEN
183  uint8_t l2addr[GNRC_NETIF_L2ADDR_MAXLEN];
184 
190  uint8_t l2addr_len;
191 #if defined(MODULE_GNRC_NETIF_DEDUP) || DOXYGEN
198 #endif
199 #endif
200 #if IS_USED(MODULE_GNRC_NETIF_6LO) || defined(DOXYGEN)
202 #endif
203 #if IS_USED(MODULE_GNRC_NETIF_PKTQ) || defined(DOXYGEN)
210 #endif
215  uint8_t cur_hl;
216  uint8_t device_type;
218 } gnrc_netif_t;
219 
235 static inline bool gnrc_netif_netdev_legacy_api(gnrc_netif_t *netif)
236 {
237  if (!IS_USED(MODULE_NETDEV_NEW_API) && !IS_USED(MODULE_NETDEV_LEGACY_API)) {
238  /* this should only happen for external netdevs or when no netdev is
239  * used (e.g. examples/networking/coap/gcoap can be used without any netdev, as still
240  * CoAP requests to ::1 can be send */
241  return true;
242  }
243 
244  if (!IS_USED(MODULE_NETDEV_NEW_API)) {
245  return true;
246  }
247 
248  if (!IS_USED(MODULE_NETDEV_LEGACY_API)) {
249  return false;
250  }
251 
252  /* both legacy and new API netdevs in use, fall back to runtime test: */
253  return (netif->dev->driver->confirm_send == NULL);
254 }
255 
264 static inline bool gnrc_netif_netdev_new_api(gnrc_netif_t *netif)
265 {
266  return !gnrc_netif_netdev_legacy_api(netif);
267 }
268 
294  int (*init)(gnrc_netif_t *netif);
295 
316  int (*send)(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt);
317 
334  gnrc_pktsnip_t *(*recv)(gnrc_netif_t *netif);
335 
350  int (*get)(gnrc_netif_t *netif, gnrc_netapi_opt_t *opt);
351 
367  int (*set)(gnrc_netif_t *netif, const gnrc_netapi_opt_t *opt);
368 
379  void (*msg_handler)(gnrc_netif_t *netif, msg_t *msg);
380 };
381 
391 
409 int gnrc_netif_create(gnrc_netif_t *netif, char *stack, int stacksize,
410  char priority, const char *name, netdev_t *dev,
411  const gnrc_netif_ops_t *ops);
412 
418 unsigned gnrc_netif_numof(void);
419 
431 static inline bool gnrc_netif_highlander(void)
432 {
433  return IS_USED(MODULE_GNRC_NETIF_SINGLE);
434 }
435 
445 
455 
477 static inline int gnrc_netif_ipv6_addrs_get(const gnrc_netif_t *netif,
478  ipv6_addr_t *addrs,
479  size_t max_len)
480 {
481  assert(netif != NULL);
482  assert(addrs != NULL);
483  assert(max_len >= sizeof(ipv6_addr_t));
484  return gnrc_netapi_get(netif->pid, NETOPT_IPV6_ADDR, 0, addrs, max_len);
485 }
486 
508 static inline int gnrc_netif_ipv6_addr_add(const gnrc_netif_t *netif,
509  const ipv6_addr_t *addr, unsigned pfx_len,
510  uint8_t flags)
511 {
512  assert(netif != NULL);
513  assert(addr != NULL);
514  assert((pfx_len > 0) && (pfx_len <= 128));
515  return gnrc_netapi_set(netif->pid, NETOPT_IPV6_ADDR,
516  ((pfx_len << 8U) | flags), addr,
517  sizeof(ipv6_addr_t));
518 }
519 
533 static inline int gnrc_netif_ipv6_addr_remove(const gnrc_netif_t *netif,
534  const ipv6_addr_t *addr)
535 {
536  assert(netif != NULL);
537  assert(addr != NULL);
539  0, addr, sizeof(ipv6_addr_t));
540 }
541 
562 static inline int gnrc_netif_ipv6_groups_get(const gnrc_netif_t *netif,
563  ipv6_addr_t *groups,
564  size_t max_len)
565 {
566  assert(netif != NULL);
567  assert(groups != NULL);
568  assert(max_len >= sizeof(ipv6_addr_t));
569  return gnrc_netapi_get(netif->pid, NETOPT_IPV6_GROUP, 0, groups, max_len);
570 }
571 
586 static inline int gnrc_netif_ipv6_group_join(const gnrc_netif_t *netif,
587  const ipv6_addr_t *group)
588 {
589  assert(netif != NULL);
590  assert(group != NULL);
591  return gnrc_netapi_set(netif->pid, NETOPT_IPV6_GROUP, 0, group,
592  sizeof(ipv6_addr_t));
593 }
594 
608 static inline int gnrc_netif_ipv6_group_leave(const gnrc_netif_t *netif,
609  const ipv6_addr_t *group)
610 {
611  assert(netif != NULL);
612  assert(group != NULL);
613  return gnrc_netapi_set(netif->pid, NETOPT_IPV6_GROUP_LEAVE, 0, group,
614  sizeof(ipv6_addr_t));
615 }
616 
626 
639 
652  const gnrc_netapi_opt_t *opt);
653 
666 
685 static inline char *gnrc_netif_addr_to_str(const uint8_t *addr, size_t addr_len, char *out)
686 {
687  return l2util_addr_to_str(addr, addr_len, out);
688 }
689 
710 static inline size_t gnrc_netif_addr_from_str(const char *str, uint8_t *out)
711 {
712  return l2util_addr_from_str(str, out);
713 }
714 
724 static inline int gnrc_netif_send(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
725 {
726  return gnrc_netapi_send(netif->pid, pkt);
727 }
728 
729 #if defined(MODULE_GNRC_NETIF_BUS) || DOXYGEN
739  gnrc_netif_bus_t type)
740 {
741  assert(type < GNRC_NETIF_BUS_NUMOF);
742  return &netif->bus[type];
743 }
744 
761  uint32_t timeout_ms);
762 #endif /* MODULE_GNRC_NETIF_BUS */
763 
764 #ifdef __cplusplus
765 }
766 #endif
767 
6LoWPAN definitions for Network interface API
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:143
Definitions low-level network driver interface.
Flag definitions for Network interface API.
IPv6 definitions for Network interface API.
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:135
netdev_type_t
Driver types for netdev.
Definition: netdev.h:306
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:250
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:228
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:149
#define GNRC_NETIF_L2ADDR_MAXLEN
Maximum length of the link-layer address.
Definition: conf.h:155
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:477
static bool gnrc_netif_netdev_new_api(gnrc_netif_t *netif)
Check if the device belonging to the given netif uses the new netdev API.
Definition: netif.h:264
static int gnrc_netif_ipv6_group_leave(const gnrc_netif_t *netif, const ipv6_addr_t *group)
Leaves an IPv6 multicast group on an interface (if IPv6 is supported)
Definition: netif.h:608
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:710
static int gnrc_netif_ipv6_group_join(const gnrc_netif_t *netif, const ipv6_addr_t *group)
Joins an IPv6 multicast group on an interface (if IPv6 is supported)
Definition: netif.h:586
gnrc_netif_t * gnrc_netif_get_by_type(netdev_type_t type, uint8_t index)
Gets an interface by the netdev type (and index)
gnrc_ipv6_event_t
Event types for GNRC_NETIF_BUS_IPV6 per-interface message bus.
Definition: netif.h:106
#define GNRC_NETIF_EVQ_NUMOF
Number of event queues.
Definition: netif.h:90
static bool gnrc_netif_netdev_legacy_api(gnrc_netif_t *netif)
Check if the device belonging to the given netif uses the legacy netdev API.
Definition: netif.h:235
int gnrc_netif_set_from_netdev(gnrc_netif_t *netif, const gnrc_netapi_opt_t *opt)
Default operation for gnrc_netif_ops_t::set()
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:724
int gnrc_netif_default_init(gnrc_netif_t *netif)
Default operation for gnrc_netif_ops_t::init()
static int gnrc_netif_ipv6_addr_add(const gnrc_netif_t *netif, const 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:508
unsigned gnrc_netif_numof(void)
Get number of network interfaces actually allocated.
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:738
bool gnrc_netif_ipv6_wait_for_global_address(gnrc_netif_t *netif, uint32_t timeout_ms)
Wait for a global address to become available.
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_bus_t
Per-Interface Event Message Buses.
Definition: netif.h:95
gnrc_netif_t * gnrc_netif_iter(const gnrc_netif_t *prev)
Iterate over all network interfaces.
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:685
int gnrc_netif_get_from_netdev(gnrc_netif_t *netif, gnrc_netapi_opt_t *opt)
Default operation for gnrc_netif_ops_t::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:562
static int gnrc_netif_ipv6_addr_remove(const gnrc_netif_t *netif, const ipv6_addr_t *addr)
Removes a (unicast or anycast) IPv6 address from an interface (if IPv6 is supported)
Definition: netif.h:533
gnrc_netif_t * gnrc_netif_get_by_pid(kernel_pid_t pid)
Get network interface by PID.
static bool gnrc_netif_highlander(void)
Check if there can only be one gnrc_netif_t interface.
Definition: netif.h:431
void gnrc_netif_init_devs(void)
Initialize all available network interfaces.
@ GNRC_IPV6_EVENT_ADDR_VALID
Address becomes valid.
Definition: netif.h:118
@ GNRC_NETIF_BUS_IPV6
provides gnrc_ipv6_event_t messages to subscribers
Definition: netif.h:97
char * l2util_addr_to_str(const uint8_t *addr, size_t addr_len, char *out)
Converts a hardware address to a human readable string.
size_t l2util_addr_from_str(const char *str, uint8_t *out)
Parses a string of colon-separated hexadecimals to a hardware address.
@ 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:160
@ NETOPT_IPV6_GROUP_LEAVE
(ipv6_addr_t) Leave an IPv6 multicast group on an interface
Definition: netopt.h:164
@ 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:132
@ NETOPT_IPV6_ADDR_REMOVE
(ipv6_addr_t) Removes an IPv6 address from an interface
Definition: netopt.h:136
Definition of net statistics.
Definitions for IPv6 addresses.
Link-layer helper function definitions.
#define IS_USED(module)
Checks whether a module is being used or not.
Definition: modules.h:67
Messaging Bus API for inter process message broadcast.
IPv6 neighbor discovery message type definitions.
Send queue for Network interface API type definitions
General definitions for network packets and their helper functions.
Generic interface to communicate with GNRC modules.
Configuration macros for Network interface API.
#define GNRC_NETIF_MSG_QUEUE_SIZE
Message queue size for network interface threads.
Definition: conf.h:179
LoRaWAN adaption for Network interface API.
Common network interface API definitions.
Definition of global configuration options.
Recursive Mutex for thread synchronization.
Scheduler API definition.
event queue structure
Definition: event.h:150
event structure
Definition: event.h:142
Data structure to be send for setting (GNRC_NETAPI_MSG_TYPE_SET) and getting (GNRC_NETAPI_MSG_TYPE_GE...
Definition: netapi.h:96
6Lo component of gnrc_netif_t
Definition: 6lo.h:49
Structure to store information on the last broadcast packet received.
Definition: dedup.h:38
IPv6 component for gnrc_netif_t.
Definition: ipv6.h:75
GNRC LoRaWAN interface descriptor.
Definition: lorawan.h:34
int(* init)(gnrc_netif_t *netif)
Initializes and registers network interface.
Definition: netif.h:294
int(* set)(gnrc_netif_t *netif, const gnrc_netapi_opt_t *opt)
Sets an option from the network interface.
Definition: netif.h:367
int(* send)(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
Send a packet over the network interface.
Definition: netif.h:316
void(* msg_handler)(gnrc_netif_t *netif, msg_t *msg)
Message handler for network interface.
Definition: netif.h:379
int(* get)(gnrc_netif_t *netif, gnrc_netapi_opt_t *opt)
Gets an option from the network interface.
Definition: netif.h:350
A packet queue for Network interface API with a de-queue timer.
Definition: type.h:33
Representation of a network interface.
Definition: netif.h:129
gnrc_netif_lorawan_t lorawan
LoRaWAN component.
Definition: netif.h:138
msg_bus_t bus[GNRC_NETIF_BUS_NUMOF]
Event Message Bus.
Definition: netif.h:144
rmutex_t mutex
Mutex of the interface.
Definition: netif.h:133
netstats_t stats
transceiver's statistics
Definition: netif.h:135
uint32_t flags
Flags for the interface.
Definition: netif.h:151
const gnrc_netif_ops_t * ops
Operations of the network interface.
Definition: netif.h:131
uint8_t device_type
Device type.
Definition: netif.h:216
gnrc_netif_6lo_t sixlo
6Lo component
Definition: netif.h:201
uint8_t cur_hl
Current hop-limit for out-going packets.
Definition: netif.h:215
netif_t netif
network interface descriptor
Definition: netif.h:130
uint8_t l2addr_len
Length in bytes of gnrc_netif_t::l2addr.
Definition: netif.h:190
kernel_pid_t pid
PID of the network interface's thread.
Definition: netif.h:217
event_t event_isr
ISR event for the network device.
Definition: netif.h:159
gnrc_netif_dedup_t last_pkt
Last received packet information.
Definition: netif.h:197
gnrc_netif_ipv6_t ipv6
IPv6 component.
Definition: netif.h:141
netdev_t * dev
Network device of the network interface.
Definition: netif.h:132
gnrc_pktsnip_t * tx_pkt
Outgoing frame that is currently transmitted.
Definition: netif.h:174
event_t event_tx_done
TX done event for the network device.
Definition: netif.h:166
gnrc_netif_pktq_t send_queue
Packet queue for sending.
Definition: netif.h:209
Type to represent parts (either headers or payload) of a packet, called snips.
Definition: pkt.h:105
A message bus is just a list of subscribers.
Definition: msg_bus.h:50
Describes a message object which can be sent between threads.
Definition: msg.h:192
int(* confirm_send)(netdev_t *dev, void *info)
Fetch the status of a transmission and perform any potential cleanup.
Definition: netdev.h:494
Structure to hold driver state.
Definition: netdev.h:365
const struct netdev_driver * driver
ptr to that driver's interface.
Definition: netdev.h:366
Network interface descriptor.
Definition: netif.h:68
Global statistics struct.
Definition: netstats.h:56
Mutex structure.
Definition: rmutex.h:34
Data type to represent an IPv6 address.
Definition: addr.h:64