25 #include <netlink-private/netlink.h>
26 #include <netlink/netlink.h>
27 #include <netlink/attr.h>
28 #include <netlink/utils.h>
29 #include <netlink/object.h>
30 #include <netlink/route/rtnl.h>
31 #include <netlink-private/route/link/api.h>
32 #include <netlink/route/link/veth.h>
34 #include <linux/if_link.h>
36 static struct nla_policy veth_policy[VETH_INFO_MAX+1] = {
37 [VETH_INFO_PEER] = { .
minlen =
sizeof(
struct ifinfomsg) },
40 static int veth_parse(
struct rtnl_link *link,
struct nlattr *data,
41 struct nlattr *xstats)
43 struct nlattr *tb[VETH_INFO_MAX+1];
44 struct nlattr *peer_tb[IFLA_MAX + 1];
48 NL_DBG(3,
"Parsing veth link info");
53 if (tb[VETH_INFO_PEER]) {
54 struct nlattr *nla_peer;
55 struct ifinfomsg *ifi;
57 nla_peer = tb[VETH_INFO_PEER];
60 peer->l_family = ifi->ifi_family;
61 peer->l_arptype = ifi->ifi_type;
62 peer->l_index = ifi->ifi_index;
63 peer->l_flags = ifi->ifi_flags;
64 peer->l_change = ifi->ifi_change;
66 nla_data(nla_peer) +
sizeof(
struct ifinfomsg),
67 nla_len(nla_peer) -
sizeof(
struct ifinfomsg),
72 err = rtnl_link_info_parse(peer, peer_tb);
94 nl_dump_line(p,
"%s\n", name);
96 nl_dump_line(p,
"%u\n", peer->l_index);
101 struct rtnl_link *dst_peer = NULL, *src_peer = src->l_info;
106 src_peer->l_info = NULL;
110 src_peer->l_info = src;
111 dst_peer->l_info = dst;
113 dst->l_info = dst_peer;
117 static int veth_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
120 struct ifinfomsg ifi;
121 struct nlattr *data, *info_peer;
123 memset(&ifi, 0,
sizeof ifi);
124 ifi.ifi_family = peer->l_family;
125 ifi.ifi_type = peer->l_arptype;
126 ifi.ifi_index = peer->l_index;
127 ifi.ifi_flags = peer->l_flags;
128 ifi.ifi_change = peer->l_change;
134 if (
nlmsg_append(msg, &ifi,
sizeof(ifi), NLMSG_ALIGNTO) < 0)
136 rtnl_link_fill_info(msg, peer);
143 static int veth_alloc(
struct rtnl_link *link)
168 static void veth_free(
struct rtnl_link *link)
180 static struct rtnl_link_info_ops veth_info_ops = {
182 .io_parse = veth_parse,
187 .io_alloc = veth_alloc,
188 .io_clone = veth_clone,
189 .io_put_attrs = veth_put_attrs,
190 .io_free = veth_free,
195 #define IS_VETH_LINK_ASSERT(link) \
196 if ((link)->l_info_ops != &veth_info_ops) { \
197 APPBUG("Link is not a veth link. set type \"veth\" first."); \
234 IS_VETH_LINK_ASSERT(link);
257 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"veth");
275 const char *peer_name, pid_t pid)
278 int err = -NLE_NOMEM;
284 if (name && peer_name) {
289 rtnl_link_set_ns_pid(peer, pid);
298 static void __init veth_init(
void)
303 static void __exit veth_exit(
void)
Dump object briefly on one line.
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
Register operations for a link info type.
Attribute validation policy.
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Dump all attributes but no statistics.
char * rtnl_link_get_name(struct rtnl_link *link)
Return name of link object.
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.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
void rtnl_link_set_name(struct rtnl_link *link, const char *name)
Set name of link object.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
int nla_len(const struct nlattr *nla)
Return length of the payload .
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, struct nla_policy *policy)
Create attribute index based on a stream of attributes.
int rtnl_link_is_veth(struct rtnl_link *link)
Check if link is a veth link.
uint16_t minlen
Minimal length of payload required.
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
struct rtnl_link * rtnl_link_veth_alloc(void)
Allocate link object of type veth.
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
void rtnl_link_veth_release(struct rtnl_link *link)
Release a veth link and its peer.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
void rtnl_link_put(struct rtnl_link *link)
Return a link object reference.
struct nl_object * nl_object_clone(struct nl_object *obj)
Allocate a new object and copy all data from an existing object.
int rtnl_link_add(struct nl_sock *sk, struct rtnl_link *link, int flags)
Add virtual link.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
struct rtnl_link * rtnl_link_veth_get_peer(struct rtnl_link *link)
Get the peer link of a veth link.