libnl: /data/src/libnl3/lib/route/link/ipvlan.c Source File

libnl 3.10.0

ipvlan.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2015 Cong Wang <cwang@twopensource.com>
4 */
5
6/**
7 * @ingroup link
8 * @defgroup ipvlan IPVLAN
9 * IP-based Virtual LAN link module
10 *
11 * @details
12 * \b Link Type Name: "ipvlan"
13 *
14 * @route_doc{link_ipvlan, IPVLAN Documentation}
15 *
16 * @{
17 */
18
19#include "nl-default.h"
20
21#include <linux/if_link.h>
22
23#include <netlink/netlink.h>
24#include <netlink/attr.h>
25#include <netlink/utils.h>
26#include <netlink/object.h>
27#include <netlink/route/rtnl.h>
28#include <netlink/route/link/ipvlan.h>
29
30#include "nl-route.h"
31#include "link-api.h"
32
33/** @cond SKIP */
34#define IPVLAN_HAS_MODE (1<<0)
35
36struct ipvlan_info
37{
38 uint16_t ipi_mode;
39 uint32_t ipi_mask;
40};
41
42/** @endcond */
43
44static struct nla_policy ipvlan_policy[IFLA_IPVLAN_MAX+1] = {
45 [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
46};
47
48static int ipvlan_alloc(struct rtnl_link *link)
49{
50 struct ipvlan_info *ipi;
51
52 if (link->l_info)
53 memset(link->l_info, 0, sizeof(*ipi));
54 else {
55 if ((ipi = calloc(1, sizeof(*ipi))) == NULL)
56 return -NLE_NOMEM;
57
58 link->l_info = ipi;
59 }
60
61 return 0;
62}
63
64static int ipvlan_parse(struct rtnl_link *link, struct nlattr *data,
65 struct nlattr *xstats)
66{
67 struct nlattr *tb[IFLA_IPVLAN_MAX+1];
68 struct ipvlan_info *ipi;
69 int err;
70
71 NL_DBG(3, "Parsing IPVLAN link info\n");
72
73 if ((err = nla_parse_nested(tb, IFLA_IPVLAN_MAX, data, ipvlan_policy)) < 0)
74 goto errout;
75
76 if ((err = ipvlan_alloc(link)) < 0)
77 goto errout;
78
79 ipi = link->l_info;
80
81 if (tb[IFLA_IPVLAN_MODE]) {
82 ipi->ipi_mode = nla_get_u16(tb[IFLA_IPVLAN_MODE]);
83 ipi->ipi_mask |= IPVLAN_HAS_MODE;
84 }
85
86 err = 0;
87errout:
88 return err;
89}
90
91static void ipvlan_free(struct rtnl_link *link)
92{
93 free(link->l_info);
94 link->l_info = NULL;
95}
96
97static void ipvlan_dump(struct rtnl_link *link, struct nl_dump_params *p)
98{
99 char buf[64];
100 struct ipvlan_info *ipi = link->l_info;
101
102 if (ipi->ipi_mask & IPVLAN_HAS_MODE) {
103 rtnl_link_ipvlan_mode2str(ipi->ipi_mode, buf, sizeof(buf));
104 nl_dump(p, "ipvlan-mode %s", buf);
105 }
106}
107
108static int ipvlan_clone(struct rtnl_link *dst, struct rtnl_link *src)
109{
110 struct ipvlan_info *vdst, *vsrc = src->l_info;
111 int err;
112
113 dst->l_info = NULL;
114 if ((err = rtnl_link_set_type(dst, "ipvlan")) < 0)
115 return err;
116 vdst = dst->l_info;
117
118 if (!vdst || !vsrc)
119 return -NLE_NOMEM;
120
121 memcpy(vdst, vsrc, sizeof(struct ipvlan_info));
122
123 return 0;
124}
125
126static int ipvlan_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
127{
128 struct ipvlan_info *ipi = link->l_info;
129 struct nlattr *data;
130
131 if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
132 return -NLE_MSGSIZE;
133
134 if (ipi->ipi_mask & IPVLAN_HAS_MODE)
135 NLA_PUT_U16(msg, IFLA_IPVLAN_MODE, ipi->ipi_mode);
136
137 nla_nest_end(msg, data);
138
139nla_put_failure:
140
141 return 0;
142}
143
144static struct rtnl_link_info_ops ipvlan_info_ops = {
145 .io_name = "ipvlan",
146 .io_alloc = ipvlan_alloc,
147 .io_parse = ipvlan_parse,
148 .io_dump = {
149 [NL_DUMP_LINE] = ipvlan_dump,
150 [NL_DUMP_DETAILS] = ipvlan_dump,
151 },
152 .io_clone = ipvlan_clone,
153 .io_put_attrs = ipvlan_put_attrs,
154 .io_free = ipvlan_free,
155};
156
157/** @cond SKIP */
158#define IS_IPVLAN_LINK_ASSERT(link) \
159 if ((link)->l_info_ops != &ipvlan_info_ops) { \
160 APPBUG("Link is not a ipvlan link. set type \"ipvlan\" first."); \
161 return -NLE_OPNOTSUPP; \
162 }
163/** @endcond */
164
165/**
166 * @name IPVLAN Object
167 * @{
168 */
169
170/**
171 * Allocate link object of type IPVLAN
172 *
173 * @return Allocated link object or NULL.
174 */
175 struct rtnl_link *rtnl_link_ipvlan_alloc(void)
176{
177 struct rtnl_link *link;
178
179 if (!(link = rtnl_link_alloc()))
180 return NULL;
181
182 if (rtnl_link_set_type(link, "ipvlan") < 0) {
183 rtnl_link_put(link);
184 return NULL;
185 }
186
187 return link;
188}
189
190/**
191 * Check if link is a IPVLAN link
192 * @arg link Link object
193 *
194 * @return True if link is a IPVLAN link, otherwise false is returned.
195 */
196 int rtnl_link_is_ipvlan(struct rtnl_link *link)
197{
198 return link->l_info_ops && !strcmp(link->l_info_ops->io_name, "ipvlan");
199}
200
201/**
202 * Set IPVLAN MODE
203 * @arg link Link object
204 * @arg mode IPVLAN mode
205 *
206 * @return 0 on success or a negative error code
207 */
208 int rtnl_link_ipvlan_set_mode(struct rtnl_link *link, uint16_t mode)
209{
210 struct ipvlan_info *ipi = link->l_info;
211
212 IS_IPVLAN_LINK_ASSERT(link);
213
214 ipi->ipi_mode = mode;
215 ipi->ipi_mask |= IPVLAN_HAS_MODE;
216
217 return 0;
218}
219
220/**
221 * Get IPVLAN Mode
222 * @arg link Link object
223 * @arg out_mode on success, return the mode
224 *
225 * @return 0 on success or a negative error code.
226 */
227 int rtnl_link_ipvlan_get_mode(struct rtnl_link *link, uint16_t *out_mode)
228{
229 struct ipvlan_info *ipi = link->l_info;
230
231 IS_IPVLAN_LINK_ASSERT(link);
232
233 if (!(ipi->ipi_mask & IPVLAN_HAS_MODE))
234 return -NLE_INVAL;
235 *out_mode = ipi->ipi_mode;
236 return 0;
237}
238
239/** @} */
240
241static const struct trans_tbl ipvlan_modes[] = {
242 __ADD(IPVLAN_MODE_L2, l2),
243 __ADD(IPVLAN_MODE_L3, l3),
244};
245
246/**
247 * @name Mode Translation
248 * @{
249 */
250
251char *rtnl_link_ipvlan_mode2str(int mode, char *buf, size_t len)
252{
253 return __type2str(mode, buf, len, ipvlan_modes, ARRAY_SIZE(ipvlan_modes));
254}
255
256int rtnl_link_ipvlan_str2mode(const char *name)
257{
258 return __str2type(name, ipvlan_modes, ARRAY_SIZE(ipvlan_modes));
259}
260
261/** @} */
262
263static void _nl_init ipvlan_init(void)
264{
265 rtnl_link_register_info(&ipvlan_info_ops);
266}
267
268static void _nl_exit ipvlan_exit(void)
269{
270 rtnl_link_unregister_info(&ipvlan_info_ops);
271}
272
273/** @} */
nla_get_u16
uint16_t nla_get_u16(const struct nlattr *nla)
Return payload of 16 bit integer attribute.
Definition attr.c:662
NLA_PUT_U16
#define NLA_PUT_U16(msg, attrtype, value)
Add 16 bit integer attribute to netlink message.
Definition attr.h:212
nla_nest_start
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
Definition attr.c:908
nla_parse_nested
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
Definition attr.c:1035
nla_nest_end
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
Definition attr.c:971
NLA_U16
@ NLA_U16
16 bit integer
Definition attr.h:36
rtnl_link_ipvlan_set_mode
int rtnl_link_ipvlan_set_mode(struct rtnl_link *link, uint16_t mode)
Set IPVLAN MODE.
Definition ipvlan.c:208
rtnl_link_ipvlan_get_mode
int rtnl_link_ipvlan_get_mode(struct rtnl_link *link, uint16_t *out_mode)
Get IPVLAN Mode.
Definition ipvlan.c:227
rtnl_link_ipvlan_alloc
struct rtnl_link * rtnl_link_ipvlan_alloc(void)
Allocate link object of type IPVLAN.
Definition ipvlan.c:175
rtnl_link_is_ipvlan
int rtnl_link_is_ipvlan(struct rtnl_link *link)
Check if link is a IPVLAN link.
Definition ipvlan.c:196
rtnl_link_register_info
int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
Register operations for a link info type.
Definition api.c:127
rtnl_link_unregister_info
int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
Unregister operations for a link info type.
Definition api.c:162
rtnl_link_alloc
struct rtnl_link * rtnl_link_alloc(void)
Allocate link object.
Definition link.c:1920
rtnl_link_put
void rtnl_link_put(struct rtnl_link *link)
Release a link object reference.
Definition link.c:1929
rtnl_link_set_type
int rtnl_link_set_type(struct rtnl_link *link, const char *type)
Set type of link object.
Definition link.c:2558
nl_dump
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition utils.c:1015
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
NL_DUMP_DETAILS
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
nl_dump_params
Dumping parameters.
Definition types.h:32
nla_policy
Attribute validation policy.
Definition attr.h:63
nla_policy::type
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition attr.h:65
rtnl_link_info_ops
Available operations to modules implementing a link info type.
Definition link-api.h:20
rtnl_link_info_ops::io_name
char * io_name
Name of link info type, must match name on kernel side.
Definition link-api.h:22
rtnl_link
Link object.
Definition nl-route.h:29