19#include <netlink-private/netlink.h>
20#include <netlink/netlink.h>
21#include <netlink/attr.h>
22#include <netlink/utils.h>
23#include <netlink/object.h>
24#include <netlink/route/rtnl.h>
25#include <netlink-private/route/link/api.h>
26#include <netlink/route/link/veth.h>
28#include <linux/if_link.h>
29#include <linux/veth.h>
31static struct nla_policy veth_policy[VETH_INFO_MAX+1] = {
32 [VETH_INFO_PEER] = { .
minlen =
sizeof(
struct ifinfomsg) },
35static int veth_parse(
struct rtnl_link *link,
struct nlattr *data,
36 struct nlattr *xstats)
38 struct nlattr *tb[VETH_INFO_MAX+1];
39 struct nlattr *peer_tb[IFLA_MAX + 1];
43 NL_DBG(3,
"Parsing veth link info\n");
48 if (tb[VETH_INFO_PEER]) {
49 struct nlattr *nla_peer;
50 struct ifinfomsg *ifi;
52 nla_peer = tb[VETH_INFO_PEER];
55 peer->l_family = ifi->ifi_family;
56 peer->l_arptype = ifi->ifi_type;
57 peer->l_index = ifi->ifi_index;
58 peer->l_flags = ifi->ifi_flags;
59 peer->l_change = ifi->ifi_change;
60 err =
nla_parse(peer_tb, IFLA_MAX, (
struct nlattr *)
61 ((
char *)
nla_data(nla_peer) +
sizeof(
struct ifinfomsg)),
62 nla_len(nla_peer) -
sizeof(
struct ifinfomsg),
67 err = rtnl_link_info_parse(peer, peer_tb);
89 nl_dump_line(p,
"%s\n", name);
91 nl_dump_line(p,
"%u\n", peer->l_index);
96 struct rtnl_link *dst_peer = NULL, *src_peer = src->l_info;
101 src_peer->l_info = NULL;
105 src_peer->l_info = src;
106 dst_peer->l_info = dst;
108 dst->l_info = dst_peer;
112static int veth_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
115 struct ifinfomsg ifi;
116 struct nlattr *data, *info_peer;
118 memset(&ifi, 0,
sizeof ifi);
119 ifi.ifi_family = peer->l_family;
120 ifi.ifi_type = peer->l_arptype;
121 ifi.ifi_index = peer->l_index;
122 ifi.ifi_flags = peer->l_flags;
123 ifi.ifi_change = peer->l_change;
129 if (
nlmsg_append(msg, &ifi,
sizeof(ifi), NLMSG_ALIGNTO) < 0)
131 rtnl_link_fill_info(msg, peer);
138static int veth_alloc(
struct rtnl_link *link)
163static void veth_free(
struct rtnl_link *link)
175static struct rtnl_link_info_ops veth_info_ops = {
177 .io_parse = veth_parse,
182 .io_alloc = veth_alloc,
183 .io_clone = veth_clone,
184 .io_put_attrs = veth_put_attrs,
185 .io_free = veth_free,
190#define IS_VETH_LINK_ASSERT(link) \
191 if ((link)->l_info_ops != &veth_info_ops) { \
192 APPBUG("Link is not a veth link. set type \"veth\" first."); \
228 IS_VETH_LINK_ASSERT(link);
251 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"veth");
269 const char *peer_name, pid_t pid)
272 int err = -NLE_NOMEM;
283 rtnl_link_set_ns_pid(peer, pid);
292static void __init veth_init(
void)
297static void __exit veth_exit(
void)
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, const struct nla_policy *policy)
Create attribute index based on a stream of attributes.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
int nla_len(const struct nlattr *nla)
Return length of the payload .
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
Register operations for a link info type.
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
int rtnl_link_add(struct nl_sock *sk, struct rtnl_link *link, int flags)
Add virtual link.
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
void rtnl_link_set_name(struct rtnl_link *link, const char *name)
Set name of link object.
char * rtnl_link_get_name(struct rtnl_link *link)
Return name of link object.
void rtnl_link_put(struct rtnl_link *link)
Return a link object reference.
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
struct nl_object * nl_object_clone(struct nl_object *obj)
Allocate a new object and copy all data from an existing object.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
struct rtnl_link * rtnl_link_veth_get_peer(struct rtnl_link *link)
Get the peer link of a veth link.
int rtnl_link_veth_add(struct nl_sock *sock, const char *name, const char *peer_name, pid_t pid)
Create a new kernel veth device.
void rtnl_link_veth_release(struct rtnl_link *link)
Release a veth link and its peer.
int rtnl_link_is_veth(struct rtnl_link *link)
Check if link is a veth link.
struct rtnl_link * rtnl_link_veth_alloc(void)
Allocate link object of type veth.
Attribute validation policy.
uint16_t minlen
Minimal length of payload required.