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