-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gnrc_netif2: provide raw adaption layer (for e.g. SLIP)
- Loading branch information
Showing
4 changed files
with
171 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (C) 2017 Freie Universität Berlin | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup net_gnrc_netif2 | ||
* @{ | ||
* | ||
* @file | ||
* @brief Raw (i.e. raw IP packets without link-layer information) adaptation | ||
* for @ref net_gnrc_netif2 | ||
* | ||
* @author Martine Lenders <[email protected]> | ||
*/ | ||
#ifndef NET_GNRC_NETIF2_RAW_H | ||
#define NET_GNRC_NETIF2_RAW_H | ||
|
||
#include "net/gnrc/netif2.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* @brief Creates a raw network interface | ||
* | ||
* @param[in] stack The stack for the network interface's thread. | ||
* @param[in] stacksize Size of @p stack. | ||
* @param[in] priority Priority for the network interface's thread. | ||
* @param[in] name Name for the network interface. May be NULL. | ||
* @param[in] dev Device for the interface. | ||
* | ||
* @see @ref gnrc_netif2_create() | ||
* | ||
* @return The network interface on success. | ||
* @return NULL, on error. | ||
*/ | ||
gnrc_netif2_t *gnrc_netif2_raw_create(char *stack, int stacksize, char priority, | ||
char *name, netdev_t *dev); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* NET_GNRC_NETIF2_RAW_H */ | ||
/** @} */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (C) 2017 Freie Universität Berlin | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @{ | ||
* | ||
* @file | ||
* @author Martine Lenders <[email protected]> | ||
*/ | ||
|
||
#include "net/gnrc/pktbuf.h" | ||
#include "net/gnrc/netif2/raw.h" | ||
|
||
#define ENABLE_DEBUG (0) | ||
#include "debug.h" | ||
|
||
#define IP_VERSION_MASK (0xf0U) | ||
#define IP_VERSION4 (0x40U) | ||
#define IP_VERSION6 (0x60U) | ||
|
||
|
||
static int _send(gnrc_netif2_t *netif, gnrc_pktsnip_t *pkt); | ||
static gnrc_pktsnip_t *_recv(gnrc_netif2_t *netif); | ||
|
||
static const gnrc_netif2_ops_t raw_ops = { | ||
.send = _send, | ||
.recv = _recv, | ||
.get = gnrc_netif2_get_from_netdev, | ||
.set = gnrc_netif2_set_from_netdev, | ||
}; | ||
|
||
gnrc_netif2_t *gnrc_netif2_raw_create(char *stack, int stacksize, | ||
char priority, char *name, | ||
netdev_t *dev) | ||
{ | ||
return gnrc_netif2_create(stack, stacksize, priority, name, dev, | ||
&raw_ops); | ||
} | ||
|
||
static inline uint8_t _get_version(uint8_t *data) | ||
{ | ||
return (data[0] & IP_VERSION_MASK); | ||
} | ||
|
||
static gnrc_pktsnip_t *_recv(gnrc_netif2_t *netif) | ||
{ | ||
netdev_t *dev = netif->dev; | ||
int bytes_expected = dev->driver->recv(dev, NULL, 0, NULL); | ||
gnrc_pktsnip_t *pkt = NULL; | ||
|
||
if (bytes_expected > 0) { | ||
int nread; | ||
|
||
pkt = gnrc_pktbuf_add(NULL, NULL, bytes_expected, GNRC_NETTYPE_UNDEF); | ||
|
||
if (!pkt) { | ||
DEBUG("gnrc_netif_raw: cannot allocate pktsnip.\n"); | ||
/* drop packet */ | ||
dev->driver->recv(dev, NULL, bytes_expected, NULL); | ||
return pkt; | ||
} | ||
nread = dev->driver->recv(dev, pkt->data, bytes_expected, NULL); | ||
if (nread <= 1) { /* we need at least 1 byte to identify IP version */ | ||
DEBUG("gnrc_netif_raw: read error.\n"); | ||
gnrc_pktbuf_release(pkt); | ||
return NULL; | ||
} | ||
if (nread < bytes_expected) { | ||
/* we've got less then the expected packet size, | ||
* so free the unused space.*/ | ||
DEBUG("gnrc_netif_raw: reallocating.\n"); | ||
gnrc_pktbuf_realloc_data(pkt, nread); | ||
} | ||
switch (_get_version(pkt->data)) { | ||
#ifdef MODULE_GNRC_IPV6 | ||
case IP_VERSION6: | ||
pkt->type = GNRC_NETTYPE_IPV6; | ||
break; | ||
#endif | ||
default: | ||
/* leave UNDEF */ | ||
break; | ||
} | ||
} | ||
return pkt; | ||
} | ||
|
||
static int _send(gnrc_netif2_t *netif, gnrc_pktsnip_t *pkt) | ||
{ | ||
gnrc_pktsnip_t *vector; | ||
int res = -ENOBUFS; | ||
size_t n; | ||
|
||
if (pkt->type == GNRC_NETTYPE_NETIF) { | ||
/* we don't need the netif snip: remove it */ | ||
pkt = gnrc_pktbuf_remove_snip(pkt, pkt); | ||
} | ||
vector = gnrc_pktbuf_get_iovec(pkt, &n); | ||
if (vector != NULL) { | ||
struct iovec *v = (struct iovec *)vector->data; | ||
netdev_t *dev = netif->dev; | ||
|
||
#ifdef MODULE_NETSTATS_L2 | ||
dev->stats.tx_unicast_count++; | ||
#endif | ||
res = dev->driver->send(dev, v, n); | ||
} | ||
return res; | ||
} | ||
|
||
/** @} */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters