Add PPP support

This patch add support for kernel PPP interfaces.

Signed-off-by: Jonas Johansson <jonasj76@gmail.com>
This commit is contained in:
Jonas Johansson 2016-10-26 16:30:52 +02:00 committed by Thomas Haller
parent 96746c469d
commit 53fd38d5d4
6 changed files with 280 additions and 2 deletions

View File

@ -65,6 +65,7 @@ nobase_libnlinclude_HEADERS = \
netlink/route/link/ipvlan.h \
netlink/route/link/vrf.h \
netlink/route/link/sriov.h \
netlink/route/link/ppp.h \
netlink/route/qdisc/cbq.h \
netlink/route/qdisc/dsmark.h \
netlink/route/qdisc/fifo.h \

View File

@ -156,6 +156,7 @@ enum {
IFLA_GSO_MAX_SEGS,
IFLA_GSO_MAX_SIZE,
IFLA_PAD,
IFLA_XDP,
__IFLA_MAX
};
@ -271,6 +272,7 @@ enum {
IFLA_BR_VLAN_DEFAULT_PVID,
IFLA_BR_PAD,
IFLA_BR_VLAN_STATS_ENABLED,
IFLA_BR_MCAST_STATS_ENABLED,
__IFLA_BR_MAX,
};
@ -314,6 +316,7 @@ enum {
IFLA_BRPORT_FLUSH,
IFLA_BRPORT_MULTICAST_ROUTER,
IFLA_BRPORT_PAD,
IFLA_BRPORT_MCAST_FLOOD,
__IFLA_BRPORT_MAX
};
#define IFLA_BRPORT_MAX (__IFLA_BRPORT_MAX - 1)
@ -459,6 +462,7 @@ enum {
enum ipvlan_mode {
IPVLAN_MODE_L2 = 0,
IPVLAN_MODE_L3,
IPVLAN_MODE_L3S,
IPVLAN_MODE_MAX
};
@ -613,7 +617,7 @@ enum {
enum {
IFLA_VF_UNSPEC,
IFLA_VF_MAC, /* Hardware queue specific attributes */
IFLA_VF_VLAN,
IFLA_VF_VLAN, /* VLAN ID and QoS */
IFLA_VF_TX_RATE, /* Max TX Bandwidth Allocation */
IFLA_VF_SPOOFCHK, /* Spoof Checking on/off switch */
IFLA_VF_LINK_STATE, /* link state enable/disable/auto switch */
@ -837,6 +841,8 @@ enum {
IFLA_STATS_UNSPEC, /* also used as 64bit pad attribute */
IFLA_STATS_LINK_64,
IFLA_STATS_LINK_XSTATS,
IFLA_STATS_LINK_XSTATS_SLAVE,
IFLA_STATS_LINK_OFFLOAD_XSTATS,
__IFLA_STATS_MAX,
};
@ -856,4 +862,23 @@ enum {
};
#define LINK_XSTATS_TYPE_MAX (__LINK_XSTATS_TYPE_MAX - 1)
/* These are stats embedded into IFLA_STATS_LINK_OFFLOAD_XSTATS */
enum {
IFLA_OFFLOAD_XSTATS_UNSPEC,
IFLA_OFFLOAD_XSTATS_CPU_HIT, /* struct rtnl_link_stats64 */
__IFLA_OFFLOAD_XSTATS_MAX
};
#define IFLA_OFFLOAD_XSTATS_MAX (__IFLA_OFFLOAD_XSTATS_MAX - 1)
/* XDP section */
enum {
IFLA_XDP_UNSPEC,
IFLA_XDP_FD,
IFLA_XDP_ATTACHED,
__IFLA_XDP_MAX,
};
#define IFLA_XDP_MAX (__IFLA_XDP_MAX - 1)
#endif /* _LINUX_IF_LINK_H */

View File

@ -0,0 +1,30 @@
/*
* netlink/route/link/ppp.h PPP Interface
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2016 Jonas Johansson <jonasj76@gmail.com>
*/
#ifndef NETLINK_LINK_PPP_H_
#define NETLINK_LINK_PPP_H_
#include <netlink/netlink.h>
#include <netlink/route/link.h>
#ifdef __cplusplus
extern "C" {
#endif
extern struct rtnl_link *rtnl_link_ppp_alloc(void);
extern int rtnl_link_ppp_set_fd(struct rtnl_link *, uint32_t);
extern uint32_t rtnl_link_ppp_get_mode(struct rtnl_link *);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -111,7 +111,7 @@ libnl_route_3_la_SOURCES = \
route/link/vxlan.c route/link/veth.c route/link/ipip.c \
route/link/ipgre.c route/link/sit.c route/link/ipvti.c \
route/link/ip6tnl.c route/link/ifb.c route/link/ipvlan.c \
route/link/vrf.c route/link/macsec.c \
route/link/vrf.c route/link/macsec.c route/link/ppp.c \
\
route/qdisc/blackhole.c route/qdisc/cbq.c route/qdisc/dsmark.c \
route/qdisc/fifo.c route/qdisc/htb.c route/qdisc/netem.c \

219
lib/route/link/ppp.c Normal file
View File

@ -0,0 +1,219 @@
/*
* lib/route/link/ppp.c PPP Link Module
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2016 Jonas Johansson <jonasj76@gmail.com>
*/
/**
* @ingroup link
* @defgroup ppp PPP
*
* @details
* \b Link Type Name: "ppp"
*
* @route_doc{link_ppp, PPP Documentation}
* @{
*/
#include <netlink-private/netlink.h>
#include <netlink/netlink.h>
#include <netlink-private/route/link/api.h>
/** @cond SKIP */
#define PPP_HAS_FD (1<<0)
struct ppp_info
{
uint32_t pi_fd;
uint32_t pi_mask;
};
/** @endcond */
static struct nla_policy ppp_nl_policy[IFLA_PPP_MAX+1] = {
[IFLA_PPP_DEV_FD] = { .type = NLA_S32 },
};
static int ppp_alloc(struct rtnl_link *link)
{
struct ppp_info *pi;
if (link->l_info)
memset(link->l_info, 0, sizeof(*pi));
else {
if ((pi = calloc(1, sizeof(*pi))) == NULL)
return -NLE_NOMEM;
link->l_info = pi;
}
return 0;
}
static int ppp_parse(struct rtnl_link *link, struct nlattr *data,
struct nlattr *xstats)
{
struct nlattr *tb[IFLA_PPP_MAX+1];
struct ppp_info *pi;
int err;
NL_DBG(3, "Parsing PPP link info\n");
if ((err = nla_parse_nested(tb, IFLA_PPP_MAX, data, ppp_nl_policy)) < 0)
goto errout;
if ((err = ppp_alloc(link)) < 0)
goto errout;
pi = link->l_info;
if (tb[IFLA_PPP_DEV_FD]) {
pi->pi_fd = nla_get_s32(tb[IFLA_PPP_DEV_FD]);
pi->pi_mask |= PPP_HAS_FD;
}
err = 0;
errout:
return err;
}
static void ppp_free(struct rtnl_link *link)
{
free(link->l_info);
link->l_info = NULL;
}
static int ppp_clone(struct rtnl_link *dst, struct rtnl_link *src)
{
struct ppp_info *vdst, *vsrc = src->l_info;
int err;
dst->l_info = NULL;
if ((err = rtnl_link_set_type(dst, "ppp")) < 0)
return err;
vdst = dst->l_info;
if (!vdst || !vsrc)
return -NLE_NOMEM;
memcpy(vdst, vsrc, sizeof(struct ppp_info));
return 0;
}
static int ppp_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
{
struct ppp_info *pi = link->l_info;
struct nlattr *data;
if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
return -NLE_MSGSIZE;
if (pi->pi_mask & PPP_HAS_FD)
NLA_PUT_S32(msg, IFLA_PPP_DEV_FD, pi->pi_fd);
nla_nest_end(msg, data);
nla_put_failure:
return 0;
}
static struct rtnl_link_info_ops ppp_info_ops = {
.io_name = "ppp",
.io_alloc = ppp_alloc,
.io_parse = ppp_parse,
.io_clone = ppp_clone,
.io_put_attrs = ppp_put_attrs,
.io_free = ppp_free,
};
/** @cond SKIP */
#define IS_PPP_LINK_ASSERT(link) \
if ((link)->l_info_ops != &ppp_info_ops) { \
APPBUG("Link is not a PPP link. set type \"ppp\" first."); \
return -NLE_OPNOTSUPP; \
}
/** @endcond */
/**
* @name PPP Object
* @{
*/
/**
* Allocate link object of type PPP
*
* @return Allocated link object or NULL.
*/
struct rtnl_link *rtnl_link_ppp_alloc(void)
{
struct rtnl_link *link;
int err;
if (!(link = rtnl_link_alloc()))
return NULL;
if ((err = rtnl_link_set_type(link, "ppp")) < 0) {
rtnl_link_put(link);
return NULL;
}
return link;
}
/**
* Set PPP file descriptor
* @arg link Link object
* @arg flags PPP file descriptor
*
* @return 0 on success or a negative error code.
*/
int rtnl_link_ppp_set_fd(struct rtnl_link *link, uint32_t fd)
{
struct ppp_info *pi = link->l_info;
IS_PPP_LINK_ASSERT(link);
pi->pi_fd |= fd;
pi->pi_mask |= PPP_HAS_FD;
return 0;
}
/**
* Get PPP file descriptor
* @arg link Link object
*
* @return PPP file descriptor, 0 if not set or a negative error code.
*/
uint32_t rtnl_link_ppp_get_mode(struct rtnl_link *link)
{
struct ppp_info *pi = link->l_info;
IS_PPP_LINK_ASSERT(link);
if (pi->pi_mask & PPP_HAS_FD)
return pi->pi_fd;
else
return 0;
}
/** @} */
static void __init ppp_init(void)
{
rtnl_link_register_info(&ppp_info_ops);
}
static void __exit ppp_exit(void)
{
rtnl_link_unregister_info(&ppp_info_ops);
}
/** @} */

View File

@ -966,6 +966,9 @@ global:
rtnl_link_macvlan_macmode2str;
rtnl_link_macvlan_set_macmode;
rtnl_link_macvlan_str2macmode;
rtnl_link_ppp_alloc;
rtnl_link_ppp_set_fd;
rtnl_link_ppp_get_fd;
rtnl_link_vxlan_get_collect_metadata;
rtnl_link_vxlan_get_flags;
rtnl_link_vxlan_get_label;