1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2016 Jonas Johansson <[email protected]>
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
28 struct ppp_info
29 {
30 int32_t pi_fd;
31 uint32_t ce_mask;
32 };
33
34 /** @endcond */
35
36 static struct nla_policy ppp_nl_policy[IFLA_PPP_MAX+1] = {
37 [IFLA_PPP_DEV_FD] = { .type = NLA_S32 },
38 };
39
ppp_alloc(struct rtnl_link * link)40 static 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
ppp_parse(struct rtnl_link * link,struct nlattr * data,struct nlattr * xstats)56 static 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;
79 errout:
80 return err;
81 }
82
ppp_free(struct rtnl_link * link)83 static void ppp_free(struct rtnl_link *link)
84 {
85 free(link->l_info);
86 link->l_info = NULL;
87 }
88
ppp_clone(struct rtnl_link * dst,struct rtnl_link * src)89 static 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
ppp_put_attrs(struct nl_msg * msg,struct rtnl_link * link)107 static 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
120 nla_put_failure:
121
122 return 0;
123 }
124
125 static 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 */
rtnl_link_ppp_alloc(void)152 struct rtnl_link *rtnl_link_ppp_alloc(void)
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 */
rtnl_link_ppp_set_fd(struct rtnl_link * link,int32_t fd)174 int 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 */
rtnl_link_ppp_get_fd(struct rtnl_link * link,int32_t * fd)192 int 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
ppp_init(void)209 static void _nl_init ppp_init(void)
210 {
211 rtnl_link_register_info(&ppp_info_ops);
212 }
213
ppp_exit(void)214 static void _nl_exit ppp_exit(void)
215 {
216 rtnl_link_unregister_info(&ppp_info_ops);
217 }
218
219 /** @} */
220