libnl 3.10.0
ppp.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2016 Jonas Johansson <jonasj76@gmail.com>
4 */
5
6/**
7 * @ingroup link
8 * @defgroup ppp PPP
9 *
10 * @details
11 * \b Link Type Name: "ppp"
12 *
13 * @route_doc{link_ppp, PPP Documentation}
14 * @{
15 */
16
17#include "nl-default.h"
18
19#include <netlink/route/link/ppp.h>
20#include <netlink/netlink.h>
21
22#include "nl-route.h"
23#include "link-api.h"
24
25/** @cond SKIP */
26#define PPP_ATTR_FD (1<<0)
27
28struct ppp_info
29{
30 int32_t pi_fd;
31 uint32_t ce_mask;
32};
33
34/** @endcond */
35
36static struct nla_policy ppp_nl_policy[IFLA_PPP_MAX+1] = {
37 [IFLA_PPP_DEV_FD] = { .type = NLA_S32 },
38};
39
40static int ppp_alloc(struct rtnl_link *link)
41{
42 struct ppp_info *info;
43
44 if (link->l_info)
45 memset(link->l_info, 0, sizeof(*info));
46 else {
47 if ((info = calloc(1, sizeof(*info))) == NULL)
48 return -NLE_NOMEM;
49
50 link->l_info = info;
51 }
52
53 return 0;
54}
55
56static int ppp_parse(struct rtnl_link *link, struct nlattr *data,
57 struct nlattr *xstats)
58{
59 struct nlattr *tb[IFLA_PPP_MAX+1];
60 struct ppp_info *info;
61 int err;
62
63 NL_DBG(3, "Parsing PPP link info\n");
64
65 if ((err = nla_parse_nested(tb, IFLA_PPP_MAX, data, ppp_nl_policy)) < 0)
66 goto errout;
67
68 if ((err = ppp_alloc(link)) < 0)
69 goto errout;
70
71 info = link->l_info;
72
73 if (tb[IFLA_PPP_DEV_FD]) {
74 info->pi_fd = nla_get_s32(tb[IFLA_PPP_DEV_FD]);
75 info->ce_mask |= PPP_ATTR_FD;
76 }
77
78 err = 0;
79errout:
80 return err;
81}
82
83static void ppp_free(struct rtnl_link *link)
84{
85 free(link->l_info);
86 link->l_info = NULL;
87}
88
89static int ppp_clone(struct rtnl_link *dst, struct rtnl_link *src)
90{
91 struct ppp_info *vdst, *vsrc = src->l_info;
92 int err;
93
94 dst->l_info = NULL;
95 if ((err = rtnl_link_set_type(dst, "ppp")) < 0)
96 return err;
97 vdst = dst->l_info;
98
99 if (!vdst || !vsrc)
100 return -NLE_NOMEM;
101
102 memcpy(vdst, vsrc, sizeof(struct ppp_info));
103
104 return 0;
105}
106
107static int ppp_put_attrs(struct nl_msg *msg, struct rtnl_link *link)
108{
109 struct ppp_info *info = link->l_info;
110 struct nlattr *data;
111
112 if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
113 return -NLE_MSGSIZE;
114
115 if (info->ce_mask & PPP_ATTR_FD)
116 NLA_PUT_S32(msg, IFLA_PPP_DEV_FD, info->pi_fd);
117
118 nla_nest_end(msg, data);
119
120nla_put_failure:
121
122 return 0;
123}
124
125static struct rtnl_link_info_ops ppp_info_ops = {
126 .io_name = "ppp",
127 .io_alloc = ppp_alloc,
128 .io_parse = ppp_parse,
129 .io_clone = ppp_clone,
130 .io_put_attrs = ppp_put_attrs,
131 .io_free = ppp_free,
132};
133
134/** @cond SKIP */
135#define IS_PPP_LINK_ASSERT(link) \
136 if ((link)->l_info_ops != &ppp_info_ops) { \
137 APPBUG("Link is not a PPP link. set type \"ppp\" first."); \
138 return -NLE_OPNOTSUPP; \
139 }
140/** @endcond */
141
142/**
143 * @name PPP Object
144 * @{
145 */
146
147/**
148 * Allocate link object of type PPP
149 *
150 * @return Allocated link object or NULL.
151 */
153{
154 struct rtnl_link *link;
155
156 if (!(link = rtnl_link_alloc()))
157 return NULL;
158
159 if (rtnl_link_set_type(link, "ppp") < 0) {
160 rtnl_link_put(link);
161 return NULL;
162 }
163
164 return link;
165}
166
167/**
168 * Set PPP file descriptor
169 * @arg link Link object
170 * @arg flags PPP file descriptor
171 *
172 * @return 0 on success or a negative error code.
173 */
174int rtnl_link_ppp_set_fd(struct rtnl_link *link, int32_t fd)
175{
176 struct ppp_info *info = link->l_info;
177
178 IS_PPP_LINK_ASSERT(link);
179
180 info->pi_fd |= fd;
181 info->ce_mask |= PPP_ATTR_FD;
182
183 return 0;
184}
185
186/**
187 * Get PPP file descriptor
188 * @arg link Link object
189 *
190 * @return PPP file descriptor, 0 if not set or a negative error code.
191 */
192int rtnl_link_ppp_get_fd(struct rtnl_link *link, int32_t *fd)
193{
194 struct ppp_info *info = link->l_info;
195
196 IS_PPP_LINK_ASSERT(link);
197
198 if (!(info->ce_mask & PPP_ATTR_FD))
199 return -NLE_NOATTR;
200
201 if (fd)
202 *fd = info->pi_fd;
203
204 return 0;
205}
206
207/** @} */
208
209static void _nl_init ppp_init(void)
210{
211 rtnl_link_register_info(&ppp_info_ops);
212}
213
214static void _nl_exit ppp_exit(void)
215{
216 rtnl_link_unregister_info(&ppp_info_ops);
217}
218
219/** @} */
#define NLA_PUT_S32(msg, attrtype, value)
Add 32 bit signed integer attribute to netlink message.
Definition attr.h:221
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
Definition attr.c:908
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
int32_t nla_get_s32(const struct nlattr *nla)
Return payload of 32 bit signed integer attribute.
Definition attr.c:687
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
Definition attr.c:971
int rtnl_link_ppp_get_fd(struct rtnl_link *link, int32_t *fd)
Get PPP file descriptor.
Definition ppp.c:192
int rtnl_link_ppp_set_fd(struct rtnl_link *link, int32_t fd)
Set PPP file descriptor.
Definition ppp.c:174
struct rtnl_link * rtnl_link_ppp_alloc(void)
Allocate link object of type PPP.
Definition ppp.c:152
Attribute validation policy.
Definition attr.h:63
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition attr.h:65