1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/*
* lib/route/link/veth.c Virtual Ethernet
*
* 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) 2013 Cong Wang <xiyou.wangcong@gmail.com>
*/
/**
* @ingroup link
* @defgroup veth VETH
* Virtual Ethernet
*
* @details
* \b Link Type Name: "veth"
*
* @route_doc{link_veth, VETH Documentation}
*
* @{
*/
#include <netlink-private/netlink.h>
#include <netlink/netlink.h>
#include <netlink/attr.h>
#include <netlink/utils.h>
#include <netlink/object.h>
#include <netlink/route/rtnl.h>
#include <netlink-private/route/link/api.h>
#include <netlink/route/link/veth.h>
#include <linux/if_link.h>
static struct nla_policy veth_policy[VETH_INFO_MAX+1] = {
[VETH_INFO_PEER] = { .minlen = sizeof(struct ifinfomsg) },
};
static int veth_parse(struct rtnl_link *link, struct nlattr *data,
struct nlattr *xstats)
{
struct nlattr *tb[VETH_INFO_MAX+1];
struct nlattr *peer_tb[IFLA_MAX + 1];
struct rtnl_link *peer = link->l_info;
int err;
NL_DBG(3, "Parsing veth link info");
if ((err = nla_parse_nested(tb, VETH_INFO_MAX, data, veth_policy)) < 0)
goto errout;
if (tb[VETH_INFO_PEER]) {
struct nlattr *nla_peer;
struct ifinfomsg *ifi;
nla_peer = tb[VETH_INFO_PEER];
ifi = nla_data(nla_peer);
peer->l_family = ifi->ifi_family;
peer->l_arptype = ifi->ifi_type;
peer->l_index = ifi->ifi_index;
peer->l_flags = ifi->ifi_flags;
peer->l_change = ifi->ifi_change;
err = nla_parse(peer_tb, IFLA_MAX,
nla_data(nla_peer) + sizeof(struct ifinfomsg),
nla_len(nla_peer) - sizeof(struct ifinfomsg),
rtln_link_policy);
if (err < 0)
goto errout;
err = rtnl_link_info_parse(peer, peer_tb);
if (err < 0)
goto errout;
}
err = 0;
errout:
return err;
}
static void veth_dump_line(struct rtnl_link *link, struct nl_dump_params *p)
{
}
static void veth_dump_details(struct rtnl_link *link, struct nl_dump_params *p)
{
struct rtnl_link *peer = link->l_info;
char *name;
name = rtnl_link_get_name(peer);
nl_dump(p, " peer ");
if (name)
nl_dump_line(p, "%s\n", name);
else
nl_dump_line(p, "%u\n", peer->l_index);
}
static int veth_clone(struct rtnl_link *dst, struct rtnl_link *src)
{
struct rtnl_link *dst_peer , *src_peer = src->l_info;
int err;
dst_peer = dst->l_info = rtnl_link_alloc();
if (!dst_peer || !src_peer)
return -NLE_NOMEM;
if ((err = rtnl_link_set_type(dst, "veth")) < 0) {
rtnl_link_put(dst_peer);
return err;
}
memcpy(dst_peer, src_peer, sizeof(struct rtnl_link));
return 0;
}
static int veth_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
{
struct rtnl_link *peer = link->l_info;
struct ifinfomsg ifi;
struct nlattr *data, *info_peer;
memset(&ifi, 0, sizeof ifi);
ifi.ifi_family = peer->l_family;
ifi.ifi_type = peer->l_arptype;
ifi.ifi_index = peer->l_index;
ifi.ifi_flags = peer->l_flags;
ifi.ifi_change = peer->l_change;
if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
return -NLE_MSGSIZE;
if (!(info_peer = nla_nest_start(msg, VETH_INFO_PEER)))
return -NLE_MSGSIZE;
if (nlmsg_append(msg, &ifi, sizeof(ifi), NLMSG_ALIGNTO) < 0)
return -NLE_MSGSIZE;
rtnl_link_fill_info(msg, peer);
nla_nest_end(msg, info_peer);
nla_nest_end(msg, data);
return 0;
}
static int veth_alloc(struct rtnl_link *link)
{
struct rtnl_link *peer;
int err;
/* return early if we are in recursion */
if (link->l_info)
return 0;
if (!(peer = rtnl_link_alloc()))
return -NLE_NOMEM;
/* We don't need to hold a reference here, as link and
* its peer should always be freed together.
*/
peer->l_info = link;
if ((err = rtnl_link_set_type(peer, "veth")) < 0) {
rtnl_link_put(peer);
return err;
}
link->l_info = peer;
return 0;
}
static struct rtnl_link_info_ops veth_info_ops = {
.io_name = "veth",
.io_parse = veth_parse,
.io_dump = {
[NL_DUMP_LINE] = veth_dump_line,
[NL_DUMP_DETAILS] = veth_dump_details,
},
.io_alloc = veth_alloc,
.io_clone = veth_clone,
.io_put_attrs = veth_put_attrs,
};
/** @cond SKIP */
#define IS_VETH_LINK_ASSERT(link) \
if ((link)->l_info_ops != &veth_info_ops) { \
APPBUG("Link is not a veth link. set type \"veth\" first."); \
return NULL; \
}
/** @endcond */
/**
* @name VETH Object
* @{
*/
/**
* Allocate link object of type veth
*
* @return Allocated link object or NULL.
*/
struct rtnl_link *rtnl_link_veth_alloc(void)
{
struct rtnl_link *link;
int err;
if (!(link = rtnl_link_alloc()))
return NULL;
if ((err = rtnl_link_set_type(link, "veth")) < 0) {
rtnl_link_put(link);
return NULL;
}
return link;
}
/**
* Get the peer link of a veth link
*
* @return the peer link object.
*/
struct rtnl_link *rtnl_link_veth_get_peer(struct rtnl_link *link)
{
IS_VETH_LINK_ASSERT(link);
return link->l_info;
}
/**
* Release a veth link and its peer
*
*/
void rtnl_link_veth_release(struct rtnl_link *link)
{
struct rtnl_link *peer = rtnl_link_veth_get_peer(link);
rtnl_link_put(peer);
rtnl_link_put(link);
}
/**
* Check if link is a veth link
* @arg link Link object
*
* @return True if link is a veth link, otherwise false is returned.
*/
int rtnl_link_is_veth(struct rtnl_link *link)
{
return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "veth");
}
/**
* Create a new kernel veth device
* @arg sock netlink socket
* @arg name name of the veth device or NULL
* @arg peer_name name of its peer or NULL
* @arg pid pid of the process in the new netns
*
* Creates a new veth device pair in the kernel and move the peer
* to the network namespace where the process is. If no name is
* provided, the kernel will automatically pick a name of the
* form "veth%d" (e.g. veth0, veth1, etc.)
*
* @return 0 on success or a negative error code
*/
int rtnl_link_veth_add(struct nl_sock *sock, const char *name,
const char *peer_name, pid_t pid)
{
struct rtnl_link *link, *peer;
int err = -NLE_NOMEM;
if (!(link = rtnl_link_veth_alloc()))
return -NLE_NOMEM;
peer = rtnl_link_veth_get_peer(link);
if (name && peer_name) {
rtnl_link_set_name(link, name);
rtnl_link_set_name(peer, peer_name);
}
rtnl_link_set_ns_pid(peer, pid);
err = rtnl_link_add(sock, link, NLM_F_CREATE);
rtnl_link_put(peer);
rtnl_link_put(link);
return err;
}
/** @} */
static void __init veth_init(void)
{
rtnl_link_register_info(&veth_info_ops);
}
static void __exit veth_exit(void)
{
rtnl_link_unregister_info(&veth_info_ops);
}
/** @} */
|