libnl  3.6.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 <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 
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 
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 
81 static void ppp_free(struct rtnl_link *link)
82 {
83  free(link->l_info);
84  link->l_info = NULL;
85 }
86 
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 
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  */
151 {
152  struct rtnl_link *link;
153  int err;
154 
155  if (!(link = rtnl_link_alloc()))
156  return NULL;
157 
158  if ((err = rtnl_link_set_type(link, "ppp")) < 0) {
159  rtnl_link_put(link);
160  return NULL;
161  }
162 
163  return link;
164 }
165 
166 /**
167  * Set PPP file descriptor
168  * @arg link Link object
169  * @arg flags PPP file descriptor
170  *
171  * @return 0 on success or a negative error code.
172  */
173 int rtnl_link_ppp_set_fd(struct rtnl_link *link, int32_t fd)
174 {
175  struct ppp_info *info = link->l_info;
176 
177  IS_PPP_LINK_ASSERT(link);
178 
179  info->pi_fd |= fd;
180  info->ce_mask |= PPP_ATTR_FD;
181 
182  return 0;
183 }
184 
185 /**
186  * Get PPP file descriptor
187  * @arg link Link object
188  *
189  * @return PPP file descriptor, 0 if not set or a negative error code.
190  */
191 int rtnl_link_ppp_get_fd(struct rtnl_link *link, int32_t *fd)
192 {
193  struct ppp_info *info = link->l_info;
194 
195  IS_PPP_LINK_ASSERT(link);
196 
197  if (!(info->ce_mask & PPP_ATTR_FD))
198  return -NLE_NOATTR;
199 
200  if (fd)
201  *fd = info->pi_fd;
202 
203  return 0;
204 }
205 
206 /** @} */
207 
208 static void __init ppp_init(void)
209 {
210  rtnl_link_register_info(&ppp_info_ops);
211 }
212 
213 static void __exit ppp_exit(void)
214 {
215  rtnl_link_unregister_info(&ppp_info_ops);
216 }
217 
218 /** @} */
#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:895
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:1013
int32_t nla_get_s32(const struct nlattr *nla)
Return payload of 32 bit signed integer attribute.
Definition: attr.c:674
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
Definition: attr.c:958
int rtnl_link_ppp_get_fd(struct rtnl_link *link, int32_t *fd)
Get PPP file descriptor.
Definition: ppp.c:191
struct rtnl_link * rtnl_link_ppp_alloc(void)
Allocate link object of type PPP.
Definition: ppp.c:150
int rtnl_link_ppp_set_fd(struct rtnl_link *link, int32_t fd)
Set PPP file descriptor.
Definition: ppp.c:173
Attribute validation policy.
Definition: attr.h:63
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition: attr.h:65