]>
Commit | Line | Data |
---|---|---|
482a8524 TG |
1 | /* |
2 | * NETLINK Generic Netlink Family | |
3 | * | |
4 | * Authors: Jamal Hadi Salim | |
5 | * Thomas Graf <tgraf@suug.ch> | |
6 | */ | |
7 | ||
482a8524 TG |
8 | #include <linux/module.h> |
9 | #include <linux/kernel.h> | |
10 | #include <linux/errno.h> | |
11 | #include <linux/types.h> | |
12 | #include <linux/socket.h> | |
13 | #include <linux/string.h> | |
14 | #include <linux/skbuff.h> | |
14cc3e2b | 15 | #include <linux/mutex.h> |
482a8524 TG |
16 | #include <net/sock.h> |
17 | #include <net/genetlink.h> | |
18 | ||
19 | struct sock *genl_sock = NULL; | |
20 | ||
14cc3e2b | 21 | static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */ |
482a8524 TG |
22 | |
23 | static void genl_lock(void) | |
24 | { | |
14cc3e2b | 25 | mutex_lock(&genl_mutex); |
482a8524 TG |
26 | } |
27 | ||
28 | static int genl_trylock(void) | |
29 | { | |
14cc3e2b | 30 | return !mutex_trylock(&genl_mutex); |
482a8524 TG |
31 | } |
32 | ||
33 | static void genl_unlock(void) | |
34 | { | |
14cc3e2b | 35 | mutex_unlock(&genl_mutex); |
482a8524 TG |
36 | |
37 | if (genl_sock && genl_sock->sk_receive_queue.qlen) | |
38 | genl_sock->sk_data_ready(genl_sock, 0); | |
39 | } | |
40 | ||
41 | #define GENL_FAM_TAB_SIZE 16 | |
42 | #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1) | |
43 | ||
44 | static struct list_head family_ht[GENL_FAM_TAB_SIZE]; | |
45 | ||
46 | static int genl_ctrl_event(int event, void *data); | |
47 | ||
48 | static inline unsigned int genl_family_hash(unsigned int id) | |
49 | { | |
50 | return id & GENL_FAM_TAB_MASK; | |
51 | } | |
52 | ||
53 | static inline struct list_head *genl_family_chain(unsigned int id) | |
54 | { | |
55 | return &family_ht[genl_family_hash(id)]; | |
56 | } | |
57 | ||
58 | static struct genl_family *genl_family_find_byid(unsigned int id) | |
59 | { | |
60 | struct genl_family *f; | |
61 | ||
62 | list_for_each_entry(f, genl_family_chain(id), family_list) | |
63 | if (f->id == id) | |
64 | return f; | |
65 | ||
66 | return NULL; | |
67 | } | |
68 | ||
69 | static struct genl_family *genl_family_find_byname(char *name) | |
70 | { | |
71 | struct genl_family *f; | |
72 | int i; | |
73 | ||
74 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) | |
75 | list_for_each_entry(f, genl_family_chain(i), family_list) | |
76 | if (strcmp(f->name, name) == 0) | |
77 | return f; | |
78 | ||
79 | return NULL; | |
80 | } | |
81 | ||
82 | static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family) | |
83 | { | |
84 | struct genl_ops *ops; | |
85 | ||
86 | list_for_each_entry(ops, &family->ops_list, ops_list) | |
87 | if (ops->cmd == cmd) | |
88 | return ops; | |
89 | ||
90 | return NULL; | |
91 | } | |
92 | ||
93 | /* Of course we are going to have problems once we hit | |
94 | * 2^16 alive types, but that can only happen by year 2K | |
95 | */ | |
96 | static inline u16 genl_generate_id(void) | |
97 | { | |
98 | static u16 id_gen_idx; | |
99 | int overflowed = 0; | |
100 | ||
101 | do { | |
102 | if (id_gen_idx == 0) | |
103 | id_gen_idx = GENL_MIN_ID; | |
104 | ||
105 | if (++id_gen_idx > GENL_MAX_ID) { | |
106 | if (!overflowed) { | |
107 | overflowed = 1; | |
108 | id_gen_idx = 0; | |
109 | continue; | |
110 | } else | |
111 | return 0; | |
112 | } | |
113 | ||
114 | } while (genl_family_find_byid(id_gen_idx)); | |
115 | ||
116 | return id_gen_idx; | |
117 | } | |
118 | ||
119 | /** | |
120 | * genl_register_ops - register generic netlink operations | |
121 | * @family: generic netlink family | |
122 | * @ops: operations to be registered | |
123 | * | |
124 | * Registers the specified operations and assigns them to the specified | |
125 | * family. Either a doit or dumpit callback must be specified or the | |
126 | * operation will fail. Only one operation structure per command | |
127 | * identifier may be registered. | |
128 | * | |
129 | * See include/net/genetlink.h for more documenation on the operations | |
130 | * structure. | |
131 | * | |
132 | * Returns 0 on success or a negative error code. | |
133 | */ | |
134 | int genl_register_ops(struct genl_family *family, struct genl_ops *ops) | |
135 | { | |
136 | int err = -EINVAL; | |
137 | ||
138 | if (ops->dumpit == NULL && ops->doit == NULL) | |
139 | goto errout; | |
140 | ||
141 | if (genl_get_cmd(ops->cmd, family)) { | |
142 | err = -EEXIST; | |
143 | goto errout; | |
144 | } | |
145 | ||
146 | genl_lock(); | |
147 | list_add_tail(&ops->ops_list, &family->ops_list); | |
148 | genl_unlock(); | |
149 | ||
150 | genl_ctrl_event(CTRL_CMD_NEWOPS, ops); | |
151 | err = 0; | |
152 | errout: | |
153 | return err; | |
154 | } | |
155 | ||
156 | /** | |
157 | * genl_unregister_ops - unregister generic netlink operations | |
158 | * @family: generic netlink family | |
159 | * @ops: operations to be unregistered | |
160 | * | |
161 | * Unregisters the specified operations and unassigns them from the | |
162 | * specified family. The operation blocks until the current message | |
163 | * processing has finished and doesn't start again until the | |
164 | * unregister process has finished. | |
165 | * | |
166 | * Note: It is not necessary to unregister all operations before | |
167 | * unregistering the family, unregistering the family will cause | |
168 | * all assigned operations to be unregistered automatically. | |
169 | * | |
170 | * Returns 0 on success or a negative error code. | |
171 | */ | |
172 | int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops) | |
173 | { | |
174 | struct genl_ops *rc; | |
175 | ||
176 | genl_lock(); | |
177 | list_for_each_entry(rc, &family->ops_list, ops_list) { | |
178 | if (rc == ops) { | |
179 | list_del(&ops->ops_list); | |
180 | genl_unlock(); | |
181 | genl_ctrl_event(CTRL_CMD_DELOPS, ops); | |
182 | return 0; | |
183 | } | |
184 | } | |
185 | genl_unlock(); | |
186 | ||
187 | return -ENOENT; | |
188 | } | |
189 | ||
190 | /** | |
191 | * genl_register_family - register a generic netlink family | |
192 | * @family: generic netlink family | |
193 | * | |
194 | * Registers the specified family after validating it first. Only one | |
195 | * family may be registered with the same family name or identifier. | |
196 | * The family id may equal GENL_ID_GENERATE causing an unique id to | |
197 | * be automatically generated and assigned. | |
198 | * | |
199 | * Return 0 on success or a negative error code. | |
200 | */ | |
201 | int genl_register_family(struct genl_family *family) | |
202 | { | |
203 | int err = -EINVAL; | |
204 | ||
205 | if (family->id && family->id < GENL_MIN_ID) | |
206 | goto errout; | |
207 | ||
208 | if (family->id > GENL_MAX_ID) | |
209 | goto errout; | |
210 | ||
211 | INIT_LIST_HEAD(&family->ops_list); | |
212 | ||
213 | genl_lock(); | |
214 | ||
215 | if (genl_family_find_byname(family->name)) { | |
216 | err = -EEXIST; | |
217 | goto errout_locked; | |
218 | } | |
219 | ||
220 | if (genl_family_find_byid(family->id)) { | |
221 | err = -EEXIST; | |
222 | goto errout_locked; | |
223 | } | |
224 | ||
482a8524 TG |
225 | if (family->id == GENL_ID_GENERATE) { |
226 | u16 newid = genl_generate_id(); | |
227 | ||
228 | if (!newid) { | |
229 | err = -ENOMEM; | |
230 | goto errout_locked; | |
231 | } | |
232 | ||
233 | family->id = newid; | |
234 | } | |
235 | ||
236 | if (family->maxattr) { | |
237 | family->attrbuf = kmalloc((family->maxattr+1) * | |
238 | sizeof(struct nlattr *), GFP_KERNEL); | |
239 | if (family->attrbuf == NULL) { | |
240 | err = -ENOMEM; | |
e200bd80 | 241 | goto errout_locked; |
482a8524 TG |
242 | } |
243 | } else | |
244 | family->attrbuf = NULL; | |
245 | ||
246 | list_add_tail(&family->family_list, genl_family_chain(family->id)); | |
247 | genl_unlock(); | |
248 | ||
249 | genl_ctrl_event(CTRL_CMD_NEWFAMILY, family); | |
250 | ||
251 | return 0; | |
252 | ||
253 | errout_locked: | |
254 | genl_unlock(); | |
255 | errout: | |
256 | return err; | |
257 | } | |
258 | ||
259 | /** | |
260 | * genl_unregister_family - unregister generic netlink family | |
261 | * @family: generic netlink family | |
262 | * | |
263 | * Unregisters the specified family. | |
264 | * | |
265 | * Returns 0 on success or a negative error code. | |
266 | */ | |
267 | int genl_unregister_family(struct genl_family *family) | |
268 | { | |
269 | struct genl_family *rc; | |
270 | ||
271 | genl_lock(); | |
272 | ||
273 | list_for_each_entry(rc, genl_family_chain(family->id), family_list) { | |
274 | if (family->id != rc->id || strcmp(rc->name, family->name)) | |
275 | continue; | |
276 | ||
277 | list_del(&rc->family_list); | |
278 | INIT_LIST_HEAD(&family->ops_list); | |
279 | genl_unlock(); | |
280 | ||
482a8524 TG |
281 | kfree(family->attrbuf); |
282 | genl_ctrl_event(CTRL_CMD_DELFAMILY, family); | |
283 | return 0; | |
284 | } | |
285 | ||
286 | genl_unlock(); | |
287 | ||
288 | return -ENOENT; | |
289 | } | |
290 | ||
e200bd80 | 291 | static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, |
482a8524 TG |
292 | int *errp) |
293 | { | |
294 | struct genl_ops *ops; | |
295 | struct genl_family *family; | |
296 | struct genl_info info; | |
297 | struct genlmsghdr *hdr = nlmsg_data(nlh); | |
298 | int hdrlen, err = -EINVAL; | |
299 | ||
300 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) | |
301 | goto ignore; | |
302 | ||
303 | if (nlh->nlmsg_type < NLMSG_MIN_TYPE) | |
304 | goto ignore; | |
305 | ||
306 | family = genl_family_find_byid(nlh->nlmsg_type); | |
307 | if (family == NULL) { | |
308 | err = -ENOENT; | |
309 | goto errout; | |
310 | } | |
311 | ||
312 | hdrlen = GENL_HDRLEN + family->hdrsize; | |
313 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) | |
314 | goto errout; | |
315 | ||
316 | ops = genl_get_cmd(hdr->cmd, family); | |
317 | if (ops == NULL) { | |
318 | err = -EOPNOTSUPP; | |
319 | goto errout; | |
320 | } | |
321 | ||
c7bdb545 | 322 | if ((ops->flags & GENL_ADMIN_PERM) && security_netlink_recv(skb, CAP_NET_ADMIN)) { |
482a8524 TG |
323 | err = -EPERM; |
324 | goto errout; | |
325 | } | |
326 | ||
327 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
328 | if (ops->dumpit == NULL) { | |
329 | err = -EOPNOTSUPP; | |
330 | goto errout; | |
331 | } | |
332 | ||
333 | *errp = err = netlink_dump_start(genl_sock, skb, nlh, | |
334 | ops->dumpit, NULL); | |
335 | if (err == 0) | |
336 | skb_pull(skb, min(NLMSG_ALIGN(nlh->nlmsg_len), | |
337 | skb->len)); | |
338 | return -1; | |
339 | } | |
340 | ||
341 | if (ops->doit == NULL) { | |
342 | err = -EOPNOTSUPP; | |
343 | goto errout; | |
344 | } | |
345 | ||
346 | if (family->attrbuf) { | |
347 | err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr, | |
348 | ops->policy); | |
349 | if (err < 0) | |
350 | goto errout; | |
351 | } | |
352 | ||
353 | info.snd_seq = nlh->nlmsg_seq; | |
354 | info.snd_pid = NETLINK_CB(skb).pid; | |
355 | info.nlhdr = nlh; | |
356 | info.genlhdr = nlmsg_data(nlh); | |
357 | info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN; | |
358 | info.attrs = family->attrbuf; | |
359 | ||
360 | *errp = err = ops->doit(skb, &info); | |
361 | return err; | |
362 | ||
363 | ignore: | |
364 | return 0; | |
365 | ||
366 | errout: | |
367 | *errp = err; | |
368 | return -1; | |
369 | } | |
370 | ||
371 | static void genl_rcv(struct sock *sk, int len) | |
372 | { | |
373 | unsigned int qlen = 0; | |
374 | ||
375 | do { | |
376 | if (genl_trylock()) | |
377 | return; | |
e200bd80 | 378 | netlink_run_queue(sk, &qlen, genl_rcv_msg); |
482a8524 TG |
379 | genl_unlock(); |
380 | } while (qlen && genl_sock && genl_sock->sk_receive_queue.qlen); | |
381 | } | |
382 | ||
383 | /************************************************************************** | |
384 | * Controller | |
385 | **************************************************************************/ | |
386 | ||
17c157c8 TG |
387 | static struct genl_family genl_ctrl = { |
388 | .id = GENL_ID_CTRL, | |
389 | .name = "nlctrl", | |
390 | .version = 0x1, | |
391 | .maxattr = CTRL_ATTR_MAX, | |
392 | }; | |
393 | ||
482a8524 TG |
394 | static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq, |
395 | u32 flags, struct sk_buff *skb, u8 cmd) | |
396 | { | |
eb328111 TG |
397 | struct nlattr *nla_ops; |
398 | struct genl_ops *ops; | |
482a8524 | 399 | void *hdr; |
eb328111 | 400 | int idx = 1; |
482a8524 | 401 | |
17c157c8 | 402 | hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd); |
482a8524 TG |
403 | if (hdr == NULL) |
404 | return -1; | |
405 | ||
406 | NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name); | |
407 | NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id); | |
eb328111 TG |
408 | NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version); |
409 | NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize); | |
410 | NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr); | |
411 | ||
412 | nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS); | |
413 | if (nla_ops == NULL) | |
414 | goto nla_put_failure; | |
415 | ||
416 | list_for_each_entry(ops, &family->ops_list, ops_list) { | |
417 | struct nlattr *nest; | |
418 | ||
419 | nest = nla_nest_start(skb, idx++); | |
420 | if (nest == NULL) | |
421 | goto nla_put_failure; | |
422 | ||
423 | NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd); | |
424 | NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags); | |
425 | ||
426 | if (ops->policy) | |
427 | NLA_PUT_FLAG(skb, CTRL_ATTR_OP_POLICY); | |
428 | ||
429 | if (ops->doit) | |
430 | NLA_PUT_FLAG(skb, CTRL_ATTR_OP_DOIT); | |
431 | ||
432 | if (ops->dumpit) | |
433 | NLA_PUT_FLAG(skb, CTRL_ATTR_OP_DUMPIT); | |
434 | ||
435 | nla_nest_end(skb, nest); | |
436 | } | |
437 | ||
438 | nla_nest_end(skb, nla_ops); | |
482a8524 TG |
439 | |
440 | return genlmsg_end(skb, hdr); | |
441 | ||
442 | nla_put_failure: | |
443 | return genlmsg_cancel(skb, hdr); | |
444 | } | |
445 | ||
446 | static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb) | |
447 | { | |
448 | ||
449 | int i, n = 0; | |
450 | struct genl_family *rt; | |
451 | int chains_to_skip = cb->args[0]; | |
452 | int fams_to_skip = cb->args[1]; | |
453 | ||
eb328111 TG |
454 | if (chains_to_skip != 0) |
455 | genl_lock(); | |
456 | ||
482a8524 TG |
457 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) { |
458 | if (i < chains_to_skip) | |
459 | continue; | |
460 | n = 0; | |
461 | list_for_each_entry(rt, genl_family_chain(i), family_list) { | |
462 | if (++n < fams_to_skip) | |
463 | continue; | |
464 | if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid, | |
465 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
466 | skb, CTRL_CMD_NEWFAMILY) < 0) | |
467 | goto errout; | |
468 | } | |
469 | ||
470 | fams_to_skip = 0; | |
471 | } | |
472 | ||
473 | errout: | |
eb328111 TG |
474 | if (chains_to_skip != 0) |
475 | genl_unlock(); | |
476 | ||
482a8524 TG |
477 | cb->args[0] = i; |
478 | cb->args[1] = n; | |
479 | ||
480 | return skb->len; | |
481 | } | |
482 | ||
483 | static struct sk_buff *ctrl_build_msg(struct genl_family *family, u32 pid, | |
b461d2f2 | 484 | int seq, u8 cmd) |
482a8524 TG |
485 | { |
486 | struct sk_buff *skb; | |
487 | int err; | |
488 | ||
339bf98f | 489 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
482a8524 TG |
490 | if (skb == NULL) |
491 | return ERR_PTR(-ENOBUFS); | |
492 | ||
493 | err = ctrl_fill_info(family, pid, seq, 0, skb, cmd); | |
494 | if (err < 0) { | |
495 | nlmsg_free(skb); | |
496 | return ERR_PTR(err); | |
497 | } | |
498 | ||
499 | return skb; | |
500 | } | |
501 | ||
502 | static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] __read_mostly = { | |
503 | [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 }, | |
5176f91e TG |
504 | [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING, |
505 | .len = GENL_NAMSIZ - 1 }, | |
482a8524 TG |
506 | }; |
507 | ||
508 | static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info) | |
509 | { | |
510 | struct sk_buff *msg; | |
511 | struct genl_family *res = NULL; | |
512 | int err = -EINVAL; | |
513 | ||
514 | if (info->attrs[CTRL_ATTR_FAMILY_ID]) { | |
515 | u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]); | |
516 | res = genl_family_find_byid(id); | |
517 | } | |
518 | ||
519 | if (info->attrs[CTRL_ATTR_FAMILY_NAME]) { | |
5176f91e | 520 | char *name; |
482a8524 | 521 | |
5176f91e | 522 | name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]); |
482a8524 TG |
523 | res = genl_family_find_byname(name); |
524 | } | |
525 | ||
526 | if (res == NULL) { | |
527 | err = -ENOENT; | |
528 | goto errout; | |
529 | } | |
530 | ||
531 | msg = ctrl_build_msg(res, info->snd_pid, info->snd_seq, | |
532 | CTRL_CMD_NEWFAMILY); | |
533 | if (IS_ERR(msg)) { | |
534 | err = PTR_ERR(msg); | |
535 | goto errout; | |
536 | } | |
537 | ||
81878d27 | 538 | err = genlmsg_reply(msg, info); |
482a8524 TG |
539 | errout: |
540 | return err; | |
541 | } | |
542 | ||
543 | static int genl_ctrl_event(int event, void *data) | |
544 | { | |
545 | struct sk_buff *msg; | |
546 | ||
547 | if (genl_sock == NULL) | |
548 | return 0; | |
549 | ||
550 | switch (event) { | |
551 | case CTRL_CMD_NEWFAMILY: | |
552 | case CTRL_CMD_DELFAMILY: | |
553 | msg = ctrl_build_msg(data, 0, 0, event); | |
554 | if (IS_ERR(msg)) | |
555 | return PTR_ERR(msg); | |
556 | ||
d387f6ad | 557 | genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL); |
482a8524 TG |
558 | break; |
559 | } | |
560 | ||
561 | return 0; | |
562 | } | |
563 | ||
564 | static struct genl_ops genl_ctrl_ops = { | |
565 | .cmd = CTRL_CMD_GETFAMILY, | |
566 | .doit = ctrl_getfamily, | |
567 | .dumpit = ctrl_dumpfamily, | |
568 | .policy = ctrl_policy, | |
569 | }; | |
570 | ||
482a8524 TG |
571 | static int __init genl_init(void) |
572 | { | |
573 | int i, err; | |
574 | ||
575 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) | |
576 | INIT_LIST_HEAD(&family_ht[i]); | |
577 | ||
578 | err = genl_register_family(&genl_ctrl); | |
579 | if (err < 0) | |
580 | goto errout; | |
581 | ||
582 | err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops); | |
583 | if (err < 0) | |
584 | goto errout_register; | |
585 | ||
586 | netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV); | |
587 | genl_sock = netlink_kernel_create(NETLINK_GENERIC, GENL_MAX_ID, | |
588 | genl_rcv, THIS_MODULE); | |
e200bd80 | 589 | if (genl_sock == NULL) |
482a8524 | 590 | panic("GENL: Cannot initialize generic netlink\n"); |
482a8524 TG |
591 | |
592 | return 0; | |
593 | ||
594 | errout_register: | |
595 | genl_unregister_family(&genl_ctrl); | |
596 | errout: | |
597 | panic("GENL: Cannot register controller: %d\n", err); | |
482a8524 TG |
598 | } |
599 | ||
600 | subsys_initcall(genl_init); | |
601 | ||
602 | EXPORT_SYMBOL(genl_sock); | |
603 | EXPORT_SYMBOL(genl_register_ops); | |
604 | EXPORT_SYMBOL(genl_unregister_ops); | |
605 | EXPORT_SYMBOL(genl_register_family); | |
606 | EXPORT_SYMBOL(genl_unregister_family); |