[scripts] env: colorize git output
[openwrt.git] / package / libnl-tiny / src / genl_ctrl.c
1 /*
2 * lib/genl/ctrl.c Generic Netlink Controller
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) 2003-2008 Thomas Graf <tgraf@suug.ch>
10 */
11
12 /**
13 * @ingroup genl_mngt
14 * @defgroup ctrl Controller
15 * @brief
16 *
17 * @{
18 */
19
20 #include <netlink-generic.h>
21 #include <netlink/netlink.h>
22 #include <netlink/genl/genl.h>
23 #include <netlink/genl/family.h>
24 #include <netlink/genl/mngt.h>
25 #include <netlink/genl/ctrl.h>
26 #include <netlink/utils.h>
27
28 /** @cond SKIP */
29 #define CTRL_VERSION 0x0001
30
31 static struct nl_cache_ops genl_ctrl_ops;
32 /** @endcond */
33
34 static int ctrl_request_update(struct nl_cache *c, struct nl_sock *h)
35 {
36 return genl_send_simple(h, GENL_ID_CTRL, CTRL_CMD_GETFAMILY,
37 CTRL_VERSION, NLM_F_DUMP);
38 }
39
40 static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
41 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
42 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_STRING,
43 .maxlen = GENL_NAMSIZ },
44 [CTRL_ATTR_VERSION] = { .type = NLA_U32 },
45 [CTRL_ATTR_HDRSIZE] = { .type = NLA_U32 },
46 [CTRL_ATTR_MAXATTR] = { .type = NLA_U32 },
47 [CTRL_ATTR_OPS] = { .type = NLA_NESTED },
48 };
49
50 static struct nla_policy family_op_policy[CTRL_ATTR_OP_MAX+1] = {
51 [CTRL_ATTR_OP_ID] = { .type = NLA_U32 },
52 [CTRL_ATTR_OP_FLAGS] = { .type = NLA_U32 },
53 };
54
55 static int ctrl_msg_parser(struct nl_cache_ops *ops, struct genl_cmd *cmd,
56 struct genl_info *info, void *arg)
57 {
58 struct genl_family *family;
59 struct nl_parser_param *pp = arg;
60 int err;
61
62 family = genl_family_alloc();
63 if (family == NULL) {
64 err = -NLE_NOMEM;
65 goto errout;
66 }
67
68 if (info->attrs[CTRL_ATTR_FAMILY_NAME] == NULL) {
69 err = -NLE_MISSING_ATTR;
70 goto errout;
71 }
72
73 if (info->attrs[CTRL_ATTR_FAMILY_ID] == NULL) {
74 err = -NLE_MISSING_ATTR;
75 goto errout;
76 }
77
78 family->ce_msgtype = info->nlh->nlmsg_type;
79 genl_family_set_id(family,
80 nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]));
81 genl_family_set_name(family,
82 nla_get_string(info->attrs[CTRL_ATTR_FAMILY_NAME]));
83
84 if (info->attrs[CTRL_ATTR_VERSION]) {
85 uint32_t version = nla_get_u32(info->attrs[CTRL_ATTR_VERSION]);
86 genl_family_set_version(family, version);
87 }
88
89 if (info->attrs[CTRL_ATTR_HDRSIZE]) {
90 uint32_t hdrsize = nla_get_u32(info->attrs[CTRL_ATTR_HDRSIZE]);
91 genl_family_set_hdrsize(family, hdrsize);
92 }
93
94 if (info->attrs[CTRL_ATTR_MAXATTR]) {
95 uint32_t maxattr = nla_get_u32(info->attrs[CTRL_ATTR_MAXATTR]);
96 genl_family_set_maxattr(family, maxattr);
97 }
98
99 if (info->attrs[CTRL_ATTR_OPS]) {
100 struct nlattr *nla, *nla_ops;
101 int remaining;
102
103 nla_ops = info->attrs[CTRL_ATTR_OPS];
104 nla_for_each_nested(nla, nla_ops, remaining) {
105 struct nlattr *tb[CTRL_ATTR_OP_MAX+1];
106 int flags = 0, id;
107
108 err = nla_parse_nested(tb, CTRL_ATTR_OP_MAX, nla,
109 family_op_policy);
110 if (err < 0)
111 goto errout;
112
113 if (tb[CTRL_ATTR_OP_ID] == NULL) {
114 err = -NLE_MISSING_ATTR;
115 goto errout;
116 }
117
118 id = nla_get_u32(tb[CTRL_ATTR_OP_ID]);
119
120 if (tb[CTRL_ATTR_OP_FLAGS])
121 flags = nla_get_u32(tb[CTRL_ATTR_OP_FLAGS]);
122
123 err = genl_family_add_op(family, id, flags);
124 if (err < 0)
125 goto errout;
126
127 }
128 }
129
130 err = pp->pp_cb((struct nl_object *) family, pp);
131 errout:
132 genl_family_put(family);
133 return err;
134 }
135
136 /**
137 * @name Cache Management
138 * @{
139 */
140
141 int genl_ctrl_alloc_cache(struct nl_sock *sock, struct nl_cache **result)
142 {
143 return nl_cache_alloc_and_fill(&genl_ctrl_ops, sock, result);
144 }
145
146 /**
147 * Look up generic netlink family by id in the provided cache.
148 * @arg cache Generic netlink family cache.
149 * @arg id Family identifier.
150 *
151 * Searches through the cache looking for a registered family
152 * matching the specified identifier. The caller will own a
153 * reference on the returned object which needs to be given
154 * back after usage using genl_family_put().
155 *
156 * @return Generic netlink family object or NULL if no match was found.
157 */
158 struct genl_family *genl_ctrl_search(struct nl_cache *cache, int id)
159 {
160 struct genl_family *fam;
161
162 if (cache->c_ops != &genl_ctrl_ops)
163 BUG();
164
165 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
166 if (fam->gf_id == id) {
167 nl_object_get((struct nl_object *) fam);
168 return fam;
169 }
170 }
171
172 return NULL;
173 }
174
175 /**
176 * @name Resolver
177 * @{
178 */
179
180 /**
181 * Look up generic netlink family by family name in the provided cache.
182 * @arg cache Generic netlink family cache.
183 * @arg name Family name.
184 *
185 * Searches through the cache looking for a registered family
186 * matching the specified name. The caller will own a reference
187 * on the returned object which needs to be given back after
188 * usage using genl_family_put().
189 *
190 * @return Generic netlink family object or NULL if no match was found.
191 */
192 struct genl_family *genl_ctrl_search_by_name(struct nl_cache *cache,
193 const char *name)
194 {
195 struct genl_family *fam;
196
197 if (cache->c_ops != &genl_ctrl_ops)
198 BUG();
199
200 nl_list_for_each_entry(fam, &cache->c_items, ce_list) {
201 if (!strcmp(name, fam->gf_name)) {
202 nl_object_get((struct nl_object *) fam);
203 return fam;
204 }
205 }
206
207 return NULL;
208 }
209
210 /** @} */
211
212 /**
213 * Resolve generic netlink family name to its identifier
214 * @arg sk Netlink socket.
215 * @arg name Name of generic netlink family
216 *
217 * Resolves the generic netlink family name to its identifer and returns
218 * it.
219 *
220 * @return A positive identifier or a negative error code.
221 */
222 int genl_ctrl_resolve(struct nl_sock *sk, const char *name)
223 {
224 struct nl_cache *cache;
225 struct genl_family *family;
226 int err;
227
228 if ((err = genl_ctrl_alloc_cache(sk, &cache)) < 0)
229 return err;
230
231 family = genl_ctrl_search_by_name(cache, name);
232 if (family == NULL) {
233 err = -NLE_OBJ_NOTFOUND;
234 goto errout;
235 }
236
237 err = genl_family_get_id(family);
238 genl_family_put(family);
239 errout:
240 nl_cache_free(cache);
241
242 return err;
243 }
244
245 /** @} */
246
247 static struct genl_cmd genl_cmds[] = {
248 {
249 .c_id = CTRL_CMD_NEWFAMILY,
250 .c_name = "NEWFAMILY" ,
251 .c_maxattr = CTRL_ATTR_MAX,
252 .c_attr_policy = ctrl_policy,
253 .c_msg_parser = ctrl_msg_parser,
254 },
255 {
256 .c_id = CTRL_CMD_DELFAMILY,
257 .c_name = "DELFAMILY" ,
258 },
259 {
260 .c_id = CTRL_CMD_GETFAMILY,
261 .c_name = "GETFAMILY" ,
262 },
263 {
264 .c_id = CTRL_CMD_NEWOPS,
265 .c_name = "NEWOPS" ,
266 },
267 {
268 .c_id = CTRL_CMD_DELOPS,
269 .c_name = "DELOPS" ,
270 },
271 };
272
273 static struct genl_ops genl_ops = {
274 .o_cmds = genl_cmds,
275 .o_ncmds = ARRAY_SIZE(genl_cmds),
276 };
277
278 /** @cond SKIP */
279 extern struct nl_object_ops genl_family_ops;
280 /** @endcond */
281
282 static struct nl_cache_ops genl_ctrl_ops = {
283 .co_name = "genl/family",
284 .co_hdrsize = GENL_HDRSIZE(0),
285 .co_msgtypes = GENL_FAMILY(GENL_ID_CTRL, "nlctrl"),
286 .co_genl = &genl_ops,
287 .co_protocol = NETLINK_GENERIC,
288 .co_request_update = ctrl_request_update,
289 .co_obj_ops = &genl_family_ops,
290 };
291
292 static void __init ctrl_init(void)
293 {
294 genl_register(&genl_ctrl_ops);
295 }
296
297 static void __exit ctrl_exit(void)
298 {
299 genl_unregister(&genl_ctrl_ops);
300 }
301
302 /** @} */
This page took 0.049734 seconds and 5 git commands to generate.