libosmocore 1.9.0.196-9975
Osmocom core library
Tun

tun network device (interface) convenience functions More...

Data Structures

struct  osmo_tundev
 

Macros

#define TUN_DEV_PATH   "/dev/net/tun"
 
#define TUN_PACKET_MAX   8196
 
#define LOGTUN(tun, lvl, fmt, args ...)
 

Functions

static int tundev_decaps (struct osmo_tundev *tundev)
 
static int tundev_read_cb (struct osmo_fd *fd)
 
static int tundev_write_cb (struct osmo_fd *fd, struct msgb *msg)
 
static int tundev_ifupdown_ind_cb (struct osmo_netdev *netdev, bool ifupdown)
 
static int tundev_dev_name_chg_cb (struct osmo_netdev *netdev, const char *new_dev_name)
 
static int tundev_mtu_chg_cb (struct osmo_netdev *netdev, uint32_t new_mtu)
 
struct osmo_tundevosmo_tundev_alloc (void *ctx, const char *name)
 Allocate a new tundev object. More...
 
void osmo_tundev_free (struct osmo_tundev *tundev)
 Free an allocated tundev object. More...
 
static int tundev_open_fd (struct osmo_tundev *tundev, int flags)
 Open and configure fd of the tunnel device. More...
 
int osmo_tundev_open (struct osmo_tundev *tundev)
 Open the tunnel device owned by the tundev object. More...
 
int osmo_tundev_close (struct osmo_tundev *tundev)
 Close the tunnel device owned by the tundev object. More...
 
bool osmo_tundev_is_open (struct osmo_tundev *tundev)
 Retrieve whether the tundev object is in "opened" state. More...
 
void osmo_tundev_set_priv_data (struct osmo_tundev *tundev, void *priv_data)
 Set private user data pointer on the tundev object. More...
 
void * osmo_tundev_get_priv_data (struct osmo_tundev *tundev)
 Get private user data pointer from the tundev object. More...
 
void osmo_tundev_set_data_ind_cb (struct osmo_tundev *tundev, osmo_tundev_data_ind_cb_t data_ind_cb)
 Set data_ind_cb callback, called when a new packet is received on the tun interface. More...
 
const char * osmo_tundev_get_name (const struct osmo_tundev *tundev)
 Get name used to identify the tundev object. More...
 
int osmo_tundev_set_dev_name (struct osmo_tundev *tundev, const char *dev_name)
 Set name used to name the tunnel interface created by the tundev object. More...
 
const char * osmo_tundev_get_dev_name (const struct osmo_tundev *tundev)
 Get name used to name the tunnel interface created by the tundev object. More...
 
int osmo_tundev_set_netns_name (struct osmo_tundev *tundev, const char *netns_name)
 Set name of the network namespace to use when opening the tunnel interface. More...
 
const char * osmo_tundev_get_netns_name (const struct osmo_tundev *tundev)
 Get name of network namespace used when opening the tunnel interface. More...
 
struct osmo_netdevosmo_tundev_get_netdev (struct osmo_tundev *tundev)
 Get netdev managing the tunnel interface of tundev. More...
 
int osmo_tundev_send (struct osmo_tundev *tundev, struct msgb *msg)
 Submit a packet to the tunnel device managed by the tundev object. More...
 

Detailed Description

tun network device (interface) convenience functions

Macro Definition Documentation

◆ LOGTUN

#define LOGTUN (   tun,
  lvl,
  fmt,
  args ... 
)
Value:
LOGP(DLGLOBAL, lvl, "TUN(%s,if=%s/%u,ns=%s): " fmt, \
(tun)->name, (tun)->dev_name ? : "", \
(tun)->ifindex, (tun)->netns_name ? : "", ## args)
const char * name
#define DLGLOBAL
global logging
Definition: logging.h:129
#define LOGP(ss, level, fmt, args...)
Log a new message through the Osmocom logging framework.
Definition: logging.h:49

◆ TUN_DEV_PATH

#define TUN_DEV_PATH   "/dev/net/tun"

◆ TUN_PACKET_MAX

#define TUN_PACKET_MAX   8196

Function Documentation

◆ osmo_tundev_alloc()

struct osmo_tundev * osmo_tundev_alloc ( void *  ctx,
const char *  name 
)

Allocate a new tundev object.

Parameters
[in]ctxtalloc context to use as a parent when allocating the tundev object
[in]nameA name providen to identify the tundev object
Returns
newly allocated tundev object on success; NULL on error

References osmo_wqueue::bfd, name, osmo_tundev::name, osmo_tundev::netdev, OSMO_FD_READ, osmo_fd_setup(), osmo_netdev_alloc(), osmo_netdev_set_dev_name_chg_cb(), osmo_netdev_set_ifupdown_ind_cb(), osmo_netdev_set_mtu_chg_cb(), osmo_netdev_set_priv_data(), osmo_wqueue_bfd_cb(), osmo_wqueue_init(), osmo_wqueue::read_cb, tundev_dev_name_chg_cb(), tundev_ifupdown_ind_cb(), tundev_mtu_chg_cb(), tundev_read_cb(), tundev_write_cb(), osmo_tundev::wqueue, and osmo_wqueue::write_cb.

◆ osmo_tundev_close()

int osmo_tundev_close ( struct osmo_tundev tundev)

Close the tunnel device owned by the tundev object.

Parameters
[in]tundevThe tundev object to close
Returns
0 on success; negative on error

References osmo_wqueue::bfd, osmo_tundev::dev_name, osmo_tundev::dev_name_dynamic, osmo_fd::fd, osmo_tundev::netdev, osmo_tundev::opened, osmo_fd_unregister(), osmo_netdev_unregister(), osmo_wqueue_clear(), and osmo_tundev::wqueue.

Referenced by osmo_tundev_free().

◆ osmo_tundev_free()

void osmo_tundev_free ( struct osmo_tundev tundev)

Free an allocated tundev object.

Parameters
[in]tundevThe tundev object to free

References osmo_tundev::netdev, osmo_netdev_free(), and osmo_tundev_close().

◆ osmo_tundev_get_dev_name()

const char * osmo_tundev_get_dev_name ( const struct osmo_tundev tundev)

Get name used to name the tunnel interface created by the tundev object.

Parameters
[in]tundevThe tundev object from where to retrieve the field
Returns
The current value of the configured tunnel interface name to use

References osmo_tundev::dev_name.

◆ osmo_tundev_get_name()

const char * osmo_tundev_get_name ( const struct osmo_tundev tundev)

Get name used to identify the tundev object.

Parameters
[in]tundevThe tundev object from where to retrieve the field
Returns
The current value of the name used to identify the tundev object

References osmo_tundev::name.

◆ osmo_tundev_get_netdev()

struct osmo_netdev * osmo_tundev_get_netdev ( struct osmo_tundev tundev)

Get netdev managing the tunnel interface of tundev.

Parameters
[in]tundevThe tundev object from where to retrieve the field
Returns
The netdev objet managing the tun interface

References osmo_tundev::netdev.

◆ osmo_tundev_get_netns_name()

const char * osmo_tundev_get_netns_name ( const struct osmo_tundev tundev)

Get name of network namespace used when opening the tunnel interface.

Parameters
[in]tundevThe tundev object from where to retrieve the field
Returns
The current value of the configured network namespace

References osmo_tundev::netns_name.

◆ osmo_tundev_get_priv_data()

void * osmo_tundev_get_priv_data ( struct osmo_tundev tundev)

Get private user data pointer from the tundev object.

Parameters
[in]tundevThe tundev object from where to retrieve the field
Returns
The current value of the priv_data field.

References osmo_tundev::priv_data.

◆ osmo_tundev_is_open()

bool osmo_tundev_is_open ( struct osmo_tundev tundev)

Retrieve whether the tundev object is in "opened" state.

Parameters
[in]tundevThe tundev object to check
Returns
true if in state "opened"; false otherwise

References osmo_tundev::opened.

◆ osmo_tundev_open()

int osmo_tundev_open ( struct osmo_tundev tundev)

◆ osmo_tundev_send()

int osmo_tundev_send ( struct osmo_tundev tundev,
struct msgb msg 
)

Submit a packet to the tunnel device managed by the tundev object.

Parameters
[in]tundevThe tundev object owning the tunnel device where to inject the packet
[in]msgThe msgb containg the packet to transfer
Returns
The current value of the configured network namespace

This function takes the ownership of msg in all cases.

References LOGL_ERROR, LOGTUN, msg, msgb_free(), osmo_wqueue_enqueue(), and osmo_tundev::wqueue.

◆ osmo_tundev_set_data_ind_cb()

void osmo_tundev_set_data_ind_cb ( struct osmo_tundev tundev,
osmo_tundev_data_ind_cb_t  data_ind_cb 
)

Set data_ind_cb callback, called when a new packet is received on the tun interface.

Parameters
[in]tundevThe tundev object where the field is set
[in]data_ind_cbthe user provided function to be called when a new packet is received

References osmo_tundev::data_ind_cb.

◆ osmo_tundev_set_dev_name()

int osmo_tundev_set_dev_name ( struct osmo_tundev tundev,
const char *  dev_name 
)

Set name used to name the tunnel interface created by the tundev object.

Parameters
[in]tundevThe tundev object where the field is set
[in]dev_nameThe tunnel interface name to use
Returns
0 on success; negative on error

This is used during osmo_tundev_open() time, and hence shouldn't be changed when the tundev object is in "opened" state. If left as NULL (default), the system will pick a suitable name during osmo_tundev_open(), and the field will be updated to the system-selected name, which can be retrieved later with osmo_tundev_get_dev_name().

References osmo_tundev::dev_name, osmo_tundev::dev_name_dynamic, osmo_tundev::opened, and osmo_talloc_replace_string().

◆ osmo_tundev_set_netns_name()

int osmo_tundev_set_netns_name ( struct osmo_tundev tundev,
const char *  netns_name 
)

Set name of the network namespace to use when opening the tunnel interface.

Parameters
[in]tundevThe tundev object where the field is set
[in]netns_nameThe network namespace to use during tunnel interface creation
Returns
0 on success; negative on error

This is used during osmo_tundev_open() time, and hence shouldn't be changed when the tundev object is in "opened" state. If left as NULL (default), the system will stay in the current network namespace.

References osmo_tundev::netns_name, osmo_tundev::opened, and osmo_talloc_replace_string().

◆ osmo_tundev_set_priv_data()

void osmo_tundev_set_priv_data ( struct osmo_tundev tundev,
void *  priv_data 
)

Set private user data pointer on the tundev object.

Parameters
[in]tundevThe tundev object where the field is set

References osmo_tundev::priv_data.

◆ tundev_decaps()

◆ tundev_dev_name_chg_cb()

static int tundev_dev_name_chg_cb ( struct osmo_netdev netdev,
const char *  new_dev_name 
)
static

◆ tundev_ifupdown_ind_cb()

static int tundev_ifupdown_ind_cb ( struct osmo_netdev netdev,
bool  ifupdown 
)
static

◆ tundev_mtu_chg_cb()

static int tundev_mtu_chg_cb ( struct osmo_netdev netdev,
uint32_t  new_mtu 
)
static

◆ tundev_open_fd()

static int tundev_open_fd ( struct osmo_tundev tundev,
int  flags 
)
static

Open and configure fd of the tunnel device.

Parameters
[in]tundevThe tundev object whose tunnel interface to open
[in]flagsinternal linux flags to pass when creating the device (not used yet)
Returns
0 on success; negative on error

References osmo_tundev::dev_name, osmo_tundev::dev_name_dynamic, flags, osmo_tundev::ifindex, LOGL_ERROR, LOGL_INFO, LOGTUN, osmo_strlcpy(), and TUN_DEV_PATH.

Referenced by osmo_tundev_open().

◆ tundev_read_cb()

static int tundev_read_cb ( struct osmo_fd fd)
static

References osmo_fd::data, and tundev_decaps().

Referenced by osmo_tundev_alloc().

◆ tundev_write_cb()

static int tundev_write_cb ( struct osmo_fd fd,
struct msgb msg 
)
static