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