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> 35 #include <linux/veth.h> 37 static struct nla_policy veth_policy[VETH_INFO_MAX+1] = {
38 [VETH_INFO_PEER] = { .
minlen =
sizeof(
struct ifinfomsg) },
41 static int veth_parse(
struct rtnl_link *link,
struct nlattr *data,
42 struct nlattr *xstats)
44 struct nlattr *tb[VETH_INFO_MAX+1];
45 struct nlattr *peer_tb[IFLA_MAX + 1];
49 NL_DBG(3,
"Parsing veth link info\n");
54 if (tb[VETH_INFO_PEER]) {
55 struct nlattr *nla_peer;
56 struct ifinfomsg *ifi;
58 nla_peer = tb[VETH_INFO_PEER];
61 peer->l_family = ifi->ifi_family;
62 peer->l_arptype = ifi->ifi_type;
63 peer->l_index = ifi->ifi_index;
64 peer->l_flags = ifi->ifi_flags;
65 peer->l_change = ifi->ifi_change;
66 err =
nla_parse(peer_tb, IFLA_MAX, (
struct nlattr *)
67 ((
char *)
nla_data(nla_peer) +
sizeof(
struct ifinfomsg)),
68 nla_len(nla_peer) -
sizeof(
struct ifinfomsg),
73 err = rtnl_link_info_parse(peer, peer_tb);
95 nl_dump_line(p,
"%s\n", name);
97 nl_dump_line(p,
"%u\n", peer->l_index);
102 struct rtnl_link *dst_peer = NULL, *src_peer = src->l_info;
107 src_peer->l_info = NULL;
111 src_peer->l_info = src;
112 dst_peer->l_info = dst;
114 dst->l_info = dst_peer;
118 static int veth_put_attrs(
struct nl_msg *msg,
struct rtnl_link *link)
121 struct ifinfomsg ifi;
122 struct nlattr *data, *info_peer;
124 memset(&ifi, 0,
sizeof ifi);
125 ifi.ifi_family = peer->l_family;
126 ifi.ifi_type = peer->l_arptype;
127 ifi.ifi_index = peer->l_index;
128 ifi.ifi_flags = peer->l_flags;
129 ifi.ifi_change = peer->l_change;
135 if (
nlmsg_append(msg, &ifi,
sizeof(ifi), NLMSG_ALIGNTO) < 0)
137 rtnl_link_fill_info(msg, peer);
144 static int veth_alloc(
struct rtnl_link *link)
169 static void veth_free(
struct rtnl_link *link)
181 static struct rtnl_link_info_ops veth_info_ops = {
183 .io_parse = veth_parse,
188 .io_alloc = veth_alloc,
189 .io_clone = veth_clone,
190 .io_put_attrs = veth_put_attrs,
191 .io_free = veth_free,
196 #define IS_VETH_LINK_ASSERT(link) \ 197 if ((link)->l_info_ops != &veth_info_ops) { \ 198 APPBUG("Link is not a veth link. set type \"veth\" first."); \ 235 IS_VETH_LINK_ASSERT(link);
258 return link->l_info_ops && !strcmp(link->l_info_ops->io_name,
"veth");
276 const char *peer_name, pid_t pid)
279 int err = -NLE_NOMEM;
290 rtnl_link_set_ns_pid(peer, pid);
299 static void __init veth_init(
void)
304 static void __exit veth_exit(
void)
Dump object briefly on one line.
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.
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, const 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 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.