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 #ifdef MODULE_GNRC_NETIF_BUS
34 #include "msg_bus.h"
35 #endif
36 #include "event.h"
37 #include "net/ipv6/addr.h"
38 #include "net/gnrc/netapi.h"
39 #include "net/gnrc/pkt.h"
40 #include "net/gnrc/netif/conf.h"
41 #if IS_USED(MODULE_GNRC_NETIF_LORAWAN)
42 #include "net/gnrc/netif/lorawan.h"
43 #endif
44 #if IS_USED(MODULE_GNRC_NETIF_6LO)
45 #include "net/gnrc/netif/6lo.h"
46 #endif
47 #if defined(MODULE_GNRC_NETIF_DEDUP) && (GNRC_NETIF_L2ADDR_MAXLEN > 0)
48 #include "net/gnrc/netif/dedup.h"
49 #endif
50 #include "net/gnrc/netif/flags.h"
51 #if IS_USED(MODULE_GNRC_NETIF_IPV6)
52 #include "net/gnrc/netif/ipv6.h"
53 #endif
54 #if IS_USED(MODULE_GNRC_NETIF_MAC)
55 #include "net/gnrc/netif/mac.h"
56 #endif
57 #include "net/ndp.h"
58 #include "net/netdev.h"
59 #include "net/netopt.h"
60 #ifdef MODULE_NETSTATS_L2
61 #include "net/netstats.h"
62 #endif
63 #include "rmutex.h"
64 #include "net/netif.h"
65 
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69 
73 typedef enum {
74 #ifdef MODULE_GNRC_IPV6
77 #endif
78  GNRC_NETIF_BUS_NUMOF
80 
84 typedef enum {
98 
103 
107 typedef struct {
112 #ifdef MODULE_NETSTATS_L2
113  netstats_t stats;
114 #endif
115 #if IS_USED(MODULE_GNRC_NETIF_LORAWAN) || defined(DOXYGEN)
117 #endif
118 #if IS_USED(MODULE_GNRC_NETIF_IPV6) || defined(DOXYGEN)
120 #endif
121 #if IS_USED(MODULE_GNRC_NETIF_MAC) || defined(DOXYGEN)
123 #endif /* IS_USED(MODULE_GNRC_NETIF_MAC) || defined(DOXYGEN) */
124 #if IS_USED(MODULE_GNRC_NETIF_BUS) || DOXYGEN
125  msg_bus_t bus[GNRC_NETIF_BUS_NUMOF];
126 #endif
127 
132  uint32_t flags;
133 #if IS_USED(MODULE_GNRC_NETIF_EVENTS) || defined(DOXYGEN)
134 
142 #endif /* MODULE_GNRC_NETIF_EVENTS */
143 #if (GNRC_NETIF_L2ADDR_MAXLEN > 0) || DOXYGEN
144 
150  uint8_t l2addr[GNRC_NETIF_L2ADDR_MAXLEN];
151 
157  uint8_t l2addr_len;
158 #if defined(MODULE_GNRC_NETIF_DEDUP) || DOXYGEN
159 
165 #endif
166 #endif
167 #if IS_USED(MODULE_GNRC_NETIF_6LO) || defined(DOXYGEN)
169 #endif
170  uint8_t cur_hl;
171  uint8_t device_type;
173 } gnrc_netif_t;
174 
194  void (*init)(gnrc_netif_t *netif);
195 
216  int (*send)(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt);
217 
234  gnrc_pktsnip_t *(*recv)(gnrc_netif_t *netif);
235 
250  int (*get)(gnrc_netif_t *netif, gnrc_netapi_opt_t *opt);
251 
267  int (*set)(gnrc_netif_t *netif, const gnrc_netapi_opt_t *opt);
268 
279  void (*msg_handler)(gnrc_netif_t *netif, msg_t *msg);
280 };
281 
290 void gnrc_netif_init_devs(void);
291 
309 int gnrc_netif_create(gnrc_netif_t *netif, char *stack, int stacksize,
310  char priority, const char *name, netdev_t *dev,
311  const gnrc_netif_ops_t *ops);
312 
318 unsigned gnrc_netif_numof(void);
319 
331 static inline bool gnrc_netif_highlander(void)
332 {
334 }
335 
345 
355 
376 static inline int gnrc_netif_ipv6_addrs_get(const gnrc_netif_t *netif,
377  ipv6_addr_t *addrs,
378  size_t max_len)
379 {
380  assert(netif != NULL);
381  assert(addrs != NULL);
382  assert(max_len >= sizeof(ipv6_addr_t));
383  return gnrc_netapi_get(netif->pid, NETOPT_IPV6_ADDR, 0, addrs, max_len);
384 }
385 
407 static inline int gnrc_netif_ipv6_addr_add(const gnrc_netif_t *netif,
408  ipv6_addr_t *addr, unsigned pfx_len,
409  uint8_t flags)
410 {
411  assert(netif != NULL);
412  assert(addr != NULL);
413  assert((pfx_len > 0) && (pfx_len <= 128));
414  return gnrc_netapi_set(netif->pid, NETOPT_IPV6_ADDR,
415  ((pfx_len << 8U) | flags), addr,
416  sizeof(ipv6_addr_t));
417 }
418 
432 static inline int gnrc_netif_ipv6_addr_remove(const gnrc_netif_t *netif,
433  ipv6_addr_t *addr)
434 {
435  assert(netif != NULL);
436  assert(addr != NULL);
438  0, addr, sizeof(ipv6_addr_t));
439 }
440 
461 static inline int gnrc_netif_ipv6_groups_get(const gnrc_netif_t *netif,
462  ipv6_addr_t *groups,
463  size_t max_len)
464 {
465  assert(netif != NULL);
466  assert(groups != NULL);
467  assert(max_len >= sizeof(ipv6_addr_t));
468  return gnrc_netapi_get(netif->pid, NETOPT_IPV6_GROUP, 0, groups, max_len);
469 }
470 
485 static inline int gnrc_netif_ipv6_group_join(const gnrc_netif_t *netif,
486  ipv6_addr_t *group)
487 {
488  assert(netif != NULL);
489  assert(group != NULL);
490  return gnrc_netapi_set(netif->pid, NETOPT_IPV6_GROUP, 0, group,
491  sizeof(ipv6_addr_t));
492 }
493 
507 static inline int gnrc_netif_ipv6_group_leave(const gnrc_netif_t *netif,
508  ipv6_addr_t *group)
509 {
510  assert(netif != NULL);
511  assert(group != NULL);
512  return gnrc_netapi_set(netif->pid, NETOPT_IPV6_GROUP_LEAVE, 0, group,
513  sizeof(ipv6_addr_t));
514 }
515 
524 
537 
550  const gnrc_netapi_opt_t *opt);
551 
568 char *gnrc_netif_addr_to_str(const uint8_t *addr, size_t addr_len, char *out);
569 
588 size_t gnrc_netif_addr_from_str(const char *str, uint8_t *out);
589 
599 static inline int gnrc_netif_send(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
600 {
601  return gnrc_netapi_send(netif->pid, pkt);
602 }
603 
604 #if defined(MODULE_GNRC_NETIF_BUS) || DOXYGEN
605 
614  gnrc_netif_bus_t type)
615 {
616  assert(type < GNRC_NETIF_BUS_NUMOF);
617  return &netif->bus[type];
618 }
619 #endif /* MODULE_GNRC_NETIF_BUS */
620 
621 #ifdef __cplusplus
622 }
623 #endif
624 
625 #endif /* NET_GNRC_NETIF_H */
626 
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:507
Types used by the kernel.
Definition of net statistics.
uint8_t cur_hl
Current hop-limit for out-going packets.
Definition: netif.h:170
void(* msg_handler)(gnrc_netif_t *netif, msg_t *msg)
Message handler for network interface.
Definition: netif.h:279
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:132
int(* send)(gnrc_netif_t *netif, gnrc_pktsnip_t *pkt)
Send a packet over the network interface.
Definition: netif.h:216
provides gnrc_ipv6_event_t messages to subscribers
Definition: netif.h:75
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:108
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
event_t event_isr
ISR event for the network device.
Definition: netif.h:141
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:613
Definitions low-level network driver interface.
Common MAC module definitions for Network interface API
Generic interface to communicate with GNRC modules.
Messaging Bus API for inter process message broadcast.
gnrc_netif_bus_t
Per-Interface Event Message Busses.
Definition: netif.h:73
msg_bus_t bus[GNRC_NETIF_BUS_NUMOF]
Event Message Bus.
Definition: netif.h:125
event structure
Definition: event.h:139
const gnrc_netif_ops_t * ops
Operations of the network interface.
Definition: netif.h:109
Mutex structure.
Definition: rmutex.h:43
A message bus is just a list of subscribers.
Definition: msg_bus.h:38
IPv6 definitions for Network interface API.
Global statistics struct.
Definition: netstats.h:43
gnrc_ipv6_event_t
Event types for GNRC_NETIF_BUS_IPV6 per-interface message bus.
Definition: netif.h:84
uint8_t device_type
Device type.
Definition: netif.h:171
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:407
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:599
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:168
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:432
Address becomes valid.
Definition: netif.h:96
uint8_t l2addr_len
Length in bytes of gnrc_netif_t::l2addr.
Definition: netif.h:157
netdev_t * dev
Network device of the network interface.
Definition: netif.h:110
IPv6 component for gnrc_netif_t.
Definition: ipv6.h:79
#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:331
#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.
event_queue_t evq
Event queue for asynchronous events.
Definition: netif.h:137
gnrc_netif_ipv6_t ipv6
IPv6 component.
Definition: netif.h:119
void(* init)(gnrc_netif_t *netif)
Initializes network interface beyond the default settings.
Definition: netif.h:194
rmutex_t mutex
Mutex of the interface.
Definition: netif.h:111
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:153
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:149
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:181
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:376
kernel_pid_t pid
PID of the network interface&#39;s thread.
Definition: netif.h:172
GNRC LoRaWAN interface descriptor.
Definition: lorawan.h:35
#define GNRC_NETIF_L2ADDR_MAXLEN
Maximum length of the link-layer address.
Definition: conf.h:141
Representation of a network interface.
Definition: netif.h:107
event queue structure
Definition: event.h:147
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:485
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:122
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:116
Flag definitions for Network interface API.
IPv6 neighbor discovery message type definitions.
#define GNRC_NETIF_SINGLE
Single interface optimizations.
Definition: conf.h:43
gnrc_netif_dedup_t last_pkt
Last received packet information.
Definition: netif.h:164
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:461
(ipv6_addr_t) get IPv6 multicast groups of an interface as array of ipv6_addr_t or join an IPv6 multi...
Definition: netopt.h:177
gnrc_netif_t * gnrc_netif_iter(const gnrc_netif_t *prev)
Iterate over all network interfaces.