Commit | Line | Data |
---|---|---|
482a8524 TG |
1 | /* |
2 | * NETLINK Generic Netlink Family | |
3 | * | |
4 | * Authors: Jamal Hadi Salim | |
5 | * Thomas Graf <tgraf@suug.ch> | |
2dbba6f7 | 6 | * Johannes Berg <johannes@sipsolutions.net> |
482a8524 TG |
7 | */ |
8 | ||
482a8524 TG |
9 | #include <linux/module.h> |
10 | #include <linux/kernel.h> | |
11 | #include <linux/errno.h> | |
12 | #include <linux/types.h> | |
13 | #include <linux/socket.h> | |
14 | #include <linux/string.h> | |
15 | #include <linux/skbuff.h> | |
14cc3e2b | 16 | #include <linux/mutex.h> |
2dbba6f7 | 17 | #include <linux/bitmap.h> |
482a8524 TG |
18 | #include <net/sock.h> |
19 | #include <net/genetlink.h> | |
20 | ||
21 | struct sock *genl_sock = NULL; | |
22 | ||
14cc3e2b | 23 | static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */ |
482a8524 TG |
24 | |
25 | static void genl_lock(void) | |
26 | { | |
14cc3e2b | 27 | mutex_lock(&genl_mutex); |
482a8524 TG |
28 | } |
29 | ||
30 | static int genl_trylock(void) | |
31 | { | |
14cc3e2b | 32 | return !mutex_trylock(&genl_mutex); |
482a8524 TG |
33 | } |
34 | ||
35 | static void genl_unlock(void) | |
36 | { | |
14cc3e2b | 37 | mutex_unlock(&genl_mutex); |
482a8524 TG |
38 | |
39 | if (genl_sock && genl_sock->sk_receive_queue.qlen) | |
40 | genl_sock->sk_data_ready(genl_sock, 0); | |
41 | } | |
42 | ||
43 | #define GENL_FAM_TAB_SIZE 16 | |
44 | #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1) | |
45 | ||
46 | static struct list_head family_ht[GENL_FAM_TAB_SIZE]; | |
2dbba6f7 JB |
47 | /* |
48 | * Bitmap of multicast groups that are currently in use. | |
49 | * | |
50 | * To avoid an allocation at boot of just one unsigned long, | |
51 | * declare it global instead. | |
52 | * Bit 0 is marked as already used since group 0 is invalid. | |
53 | */ | |
54 | static unsigned long mc_group_start = 0x1; | |
55 | static unsigned long *mc_groups = &mc_group_start; | |
56 | static unsigned long mc_groups_longs = 1; | |
482a8524 TG |
57 | |
58 | static int genl_ctrl_event(int event, void *data); | |
59 | ||
60 | static inline unsigned int genl_family_hash(unsigned int id) | |
61 | { | |
62 | return id & GENL_FAM_TAB_MASK; | |
63 | } | |
64 | ||
65 | static inline struct list_head *genl_family_chain(unsigned int id) | |
66 | { | |
67 | return &family_ht[genl_family_hash(id)]; | |
68 | } | |
69 | ||
70 | static struct genl_family *genl_family_find_byid(unsigned int id) | |
71 | { | |
72 | struct genl_family *f; | |
73 | ||
74 | list_for_each_entry(f, genl_family_chain(id), family_list) | |
75 | if (f->id == id) | |
76 | return f; | |
77 | ||
78 | return NULL; | |
79 | } | |
80 | ||
81 | static struct genl_family *genl_family_find_byname(char *name) | |
82 | { | |
83 | struct genl_family *f; | |
84 | int i; | |
85 | ||
86 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) | |
87 | list_for_each_entry(f, genl_family_chain(i), family_list) | |
88 | if (strcmp(f->name, name) == 0) | |
89 | return f; | |
90 | ||
91 | return NULL; | |
92 | } | |
93 | ||
94 | static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family) | |
95 | { | |
96 | struct genl_ops *ops; | |
97 | ||
98 | list_for_each_entry(ops, &family->ops_list, ops_list) | |
99 | if (ops->cmd == cmd) | |
100 | return ops; | |
101 | ||
102 | return NULL; | |
103 | } | |
104 | ||
105 | /* Of course we are going to have problems once we hit | |
106 | * 2^16 alive types, but that can only happen by year 2K | |
107 | */ | |
108 | static inline u16 genl_generate_id(void) | |
109 | { | |
110 | static u16 id_gen_idx; | |
111 | int overflowed = 0; | |
112 | ||
113 | do { | |
114 | if (id_gen_idx == 0) | |
115 | id_gen_idx = GENL_MIN_ID; | |
116 | ||
117 | if (++id_gen_idx > GENL_MAX_ID) { | |
118 | if (!overflowed) { | |
119 | overflowed = 1; | |
120 | id_gen_idx = 0; | |
121 | continue; | |
122 | } else | |
123 | return 0; | |
124 | } | |
125 | ||
126 | } while (genl_family_find_byid(id_gen_idx)); | |
127 | ||
128 | return id_gen_idx; | |
129 | } | |
130 | ||
2dbba6f7 JB |
131 | static struct genl_multicast_group notify_grp; |
132 | ||
133 | /** | |
134 | * genl_register_mc_group - register a multicast group | |
135 | * | |
136 | * Registers the specified multicast group and notifies userspace | |
137 | * about the new group. | |
138 | * | |
139 | * Returns 0 on success or a negative error code. | |
140 | * | |
141 | * @family: The generic netlink family the group shall be registered for. | |
142 | * @grp: The group to register, must have a name. | |
143 | */ | |
144 | int genl_register_mc_group(struct genl_family *family, | |
145 | struct genl_multicast_group *grp) | |
146 | { | |
147 | int id; | |
148 | unsigned long *new_groups; | |
149 | int err; | |
150 | ||
151 | BUG_ON(grp->name[0] == '\0'); | |
152 | ||
153 | genl_lock(); | |
154 | ||
155 | /* special-case our own group */ | |
156 | if (grp == ¬ify_grp) | |
157 | id = GENL_ID_CTRL; | |
158 | else | |
159 | id = find_first_zero_bit(mc_groups, | |
160 | mc_groups_longs * BITS_PER_LONG); | |
161 | ||
162 | ||
163 | if (id >= mc_groups_longs * BITS_PER_LONG) { | |
164 | size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long); | |
165 | ||
166 | if (mc_groups == &mc_group_start) { | |
167 | new_groups = kzalloc(nlen, GFP_KERNEL); | |
168 | if (!new_groups) { | |
169 | err = -ENOMEM; | |
170 | goto out; | |
171 | } | |
172 | mc_groups = new_groups; | |
173 | *mc_groups = mc_group_start; | |
174 | } else { | |
175 | new_groups = krealloc(mc_groups, nlen, GFP_KERNEL); | |
176 | if (!new_groups) { | |
177 | err = -ENOMEM; | |
178 | goto out; | |
179 | } | |
180 | mc_groups = new_groups; | |
181 | mc_groups[mc_groups_longs] = 0; | |
182 | } | |
183 | mc_groups_longs++; | |
184 | } | |
185 | ||
186 | err = netlink_change_ngroups(genl_sock, | |
187 | sizeof(unsigned long) * NETLINK_GENERIC); | |
188 | if (err) | |
189 | goto out; | |
190 | ||
191 | grp->id = id; | |
192 | set_bit(id, mc_groups); | |
193 | list_add_tail(&grp->list, &family->mcast_groups); | |
194 | grp->family = family; | |
195 | ||
196 | genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp); | |
197 | out: | |
198 | genl_unlock(); | |
199 | return 0; | |
200 | } | |
201 | EXPORT_SYMBOL(genl_register_mc_group); | |
202 | ||
203 | /** | |
204 | * genl_unregister_mc_group - unregister a multicast group | |
205 | * | |
206 | * Unregisters the specified multicast group and notifies userspace | |
207 | * about it. All current listeners on the group are removed. | |
208 | * | |
209 | * Note: It is not necessary to unregister all multicast groups before | |
210 | * unregistering the family, unregistering the family will cause | |
211 | * all assigned multicast groups to be unregistered automatically. | |
212 | * | |
213 | * @family: Generic netlink family the group belongs to. | |
214 | * @grp: The group to unregister, must have been registered successfully | |
215 | * previously. | |
216 | */ | |
217 | void genl_unregister_mc_group(struct genl_family *family, | |
218 | struct genl_multicast_group *grp) | |
219 | { | |
220 | BUG_ON(grp->family != family); | |
221 | genl_lock(); | |
222 | netlink_clear_multicast_users(genl_sock, grp->id); | |
223 | clear_bit(grp->id, mc_groups); | |
224 | list_del(&grp->list); | |
225 | genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp); | |
226 | grp->id = 0; | |
227 | grp->family = NULL; | |
228 | genl_unlock(); | |
229 | } | |
230 | ||
231 | static void genl_unregister_mc_groups(struct genl_family *family) | |
232 | { | |
233 | struct genl_multicast_group *grp, *tmp; | |
234 | ||
235 | list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list) | |
236 | genl_unregister_mc_group(family, grp); | |
237 | } | |
238 | ||
482a8524 TG |
239 | /** |
240 | * genl_register_ops - register generic netlink operations | |
241 | * @family: generic netlink family | |
242 | * @ops: operations to be registered | |
243 | * | |
244 | * Registers the specified operations and assigns them to the specified | |
245 | * family. Either a doit or dumpit callback must be specified or the | |
246 | * operation will fail. Only one operation structure per command | |
247 | * identifier may be registered. | |
248 | * | |
249 | * See include/net/genetlink.h for more documenation on the operations | |
250 | * structure. | |
251 | * | |
252 | * Returns 0 on success or a negative error code. | |
253 | */ | |
254 | int genl_register_ops(struct genl_family *family, struct genl_ops *ops) | |
255 | { | |
256 | int err = -EINVAL; | |
257 | ||
258 | if (ops->dumpit == NULL && ops->doit == NULL) | |
259 | goto errout; | |
260 | ||
261 | if (genl_get_cmd(ops->cmd, family)) { | |
262 | err = -EEXIST; | |
263 | goto errout; | |
264 | } | |
265 | ||
334c29a6 | 266 | if (ops->dumpit) |
334c29a6 | 267 | ops->flags |= GENL_CMD_CAP_DUMP; |
48d4ed7a JHS |
268 | if (ops->doit) |
269 | ops->flags |= GENL_CMD_CAP_DO; | |
334c29a6 JHS |
270 | if (ops->policy) |
271 | ops->flags |= GENL_CMD_CAP_HASPOL; | |
272 | ||
482a8524 TG |
273 | genl_lock(); |
274 | list_add_tail(&ops->ops_list, &family->ops_list); | |
275 | genl_unlock(); | |
276 | ||
277 | genl_ctrl_event(CTRL_CMD_NEWOPS, ops); | |
278 | err = 0; | |
279 | errout: | |
280 | return err; | |
281 | } | |
282 | ||
283 | /** | |
284 | * genl_unregister_ops - unregister generic netlink operations | |
285 | * @family: generic netlink family | |
286 | * @ops: operations to be unregistered | |
287 | * | |
288 | * Unregisters the specified operations and unassigns them from the | |
289 | * specified family. The operation blocks until the current message | |
290 | * processing has finished and doesn't start again until the | |
291 | * unregister process has finished. | |
292 | * | |
293 | * Note: It is not necessary to unregister all operations before | |
294 | * unregistering the family, unregistering the family will cause | |
295 | * all assigned operations to be unregistered automatically. | |
296 | * | |
297 | * Returns 0 on success or a negative error code. | |
298 | */ | |
299 | int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops) | |
300 | { | |
301 | struct genl_ops *rc; | |
302 | ||
303 | genl_lock(); | |
304 | list_for_each_entry(rc, &family->ops_list, ops_list) { | |
305 | if (rc == ops) { | |
306 | list_del(&ops->ops_list); | |
307 | genl_unlock(); | |
308 | genl_ctrl_event(CTRL_CMD_DELOPS, ops); | |
309 | return 0; | |
310 | } | |
311 | } | |
312 | genl_unlock(); | |
313 | ||
314 | return -ENOENT; | |
315 | } | |
316 | ||
317 | /** | |
318 | * genl_register_family - register a generic netlink family | |
319 | * @family: generic netlink family | |
320 | * | |
321 | * Registers the specified family after validating it first. Only one | |
322 | * family may be registered with the same family name or identifier. | |
323 | * The family id may equal GENL_ID_GENERATE causing an unique id to | |
324 | * be automatically generated and assigned. | |
325 | * | |
326 | * Return 0 on success or a negative error code. | |
327 | */ | |
328 | int genl_register_family(struct genl_family *family) | |
329 | { | |
330 | int err = -EINVAL; | |
331 | ||
332 | if (family->id && family->id < GENL_MIN_ID) | |
333 | goto errout; | |
334 | ||
335 | if (family->id > GENL_MAX_ID) | |
336 | goto errout; | |
337 | ||
338 | INIT_LIST_HEAD(&family->ops_list); | |
2dbba6f7 | 339 | INIT_LIST_HEAD(&family->mcast_groups); |
482a8524 TG |
340 | |
341 | genl_lock(); | |
342 | ||
343 | if (genl_family_find_byname(family->name)) { | |
344 | err = -EEXIST; | |
345 | goto errout_locked; | |
346 | } | |
347 | ||
348 | if (genl_family_find_byid(family->id)) { | |
349 | err = -EEXIST; | |
350 | goto errout_locked; | |
351 | } | |
352 | ||
482a8524 TG |
353 | if (family->id == GENL_ID_GENERATE) { |
354 | u16 newid = genl_generate_id(); | |
355 | ||
356 | if (!newid) { | |
357 | err = -ENOMEM; | |
358 | goto errout_locked; | |
359 | } | |
360 | ||
361 | family->id = newid; | |
362 | } | |
363 | ||
364 | if (family->maxattr) { | |
365 | family->attrbuf = kmalloc((family->maxattr+1) * | |
366 | sizeof(struct nlattr *), GFP_KERNEL); | |
367 | if (family->attrbuf == NULL) { | |
368 | err = -ENOMEM; | |
e200bd80 | 369 | goto errout_locked; |
482a8524 TG |
370 | } |
371 | } else | |
372 | family->attrbuf = NULL; | |
373 | ||
374 | list_add_tail(&family->family_list, genl_family_chain(family->id)); | |
375 | genl_unlock(); | |
376 | ||
377 | genl_ctrl_event(CTRL_CMD_NEWFAMILY, family); | |
378 | ||
379 | return 0; | |
380 | ||
381 | errout_locked: | |
382 | genl_unlock(); | |
383 | errout: | |
384 | return err; | |
385 | } | |
386 | ||
387 | /** | |
388 | * genl_unregister_family - unregister generic netlink family | |
389 | * @family: generic netlink family | |
390 | * | |
391 | * Unregisters the specified family. | |
392 | * | |
393 | * Returns 0 on success or a negative error code. | |
394 | */ | |
395 | int genl_unregister_family(struct genl_family *family) | |
396 | { | |
397 | struct genl_family *rc; | |
398 | ||
2dbba6f7 JB |
399 | genl_unregister_mc_groups(family); |
400 | ||
482a8524 TG |
401 | genl_lock(); |
402 | ||
403 | list_for_each_entry(rc, genl_family_chain(family->id), family_list) { | |
404 | if (family->id != rc->id || strcmp(rc->name, family->name)) | |
405 | continue; | |
406 | ||
407 | list_del(&rc->family_list); | |
408 | INIT_LIST_HEAD(&family->ops_list); | |
409 | genl_unlock(); | |
410 | ||
482a8524 TG |
411 | kfree(family->attrbuf); |
412 | genl_ctrl_event(CTRL_CMD_DELFAMILY, family); | |
413 | return 0; | |
414 | } | |
415 | ||
416 | genl_unlock(); | |
417 | ||
418 | return -ENOENT; | |
419 | } | |
420 | ||
1d00a4eb | 421 | static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
482a8524 TG |
422 | { |
423 | struct genl_ops *ops; | |
424 | struct genl_family *family; | |
425 | struct genl_info info; | |
426 | struct genlmsghdr *hdr = nlmsg_data(nlh); | |
1d00a4eb | 427 | int hdrlen, err; |
482a8524 | 428 | |
746fac4d | 429 | family = genl_family_find_byid(nlh->nlmsg_type); |
1d00a4eb TG |
430 | if (family == NULL) |
431 | return -ENOENT; | |
482a8524 TG |
432 | |
433 | hdrlen = GENL_HDRLEN + family->hdrsize; | |
434 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) | |
1d00a4eb | 435 | return -EINVAL; |
482a8524 TG |
436 | |
437 | ops = genl_get_cmd(hdr->cmd, family); | |
1d00a4eb TG |
438 | if (ops == NULL) |
439 | return -EOPNOTSUPP; | |
482a8524 | 440 | |
1d00a4eb TG |
441 | if ((ops->flags & GENL_ADMIN_PERM) && |
442 | security_netlink_recv(skb, CAP_NET_ADMIN)) | |
443 | return -EPERM; | |
482a8524 TG |
444 | |
445 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1d00a4eb TG |
446 | if (ops->dumpit == NULL) |
447 | return -EOPNOTSUPP; | |
482a8524 | 448 | |
c702e804 TG |
449 | return netlink_dump_start(genl_sock, skb, nlh, |
450 | ops->dumpit, ops->done); | |
482a8524 TG |
451 | } |
452 | ||
1d00a4eb TG |
453 | if (ops->doit == NULL) |
454 | return -EOPNOTSUPP; | |
482a8524 TG |
455 | |
456 | if (family->attrbuf) { | |
457 | err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr, | |
458 | ops->policy); | |
459 | if (err < 0) | |
1d00a4eb | 460 | return err; |
482a8524 TG |
461 | } |
462 | ||
463 | info.snd_seq = nlh->nlmsg_seq; | |
464 | info.snd_pid = NETLINK_CB(skb).pid; | |
465 | info.nlhdr = nlh; | |
466 | info.genlhdr = nlmsg_data(nlh); | |
467 | info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN; | |
468 | info.attrs = family->attrbuf; | |
469 | ||
1d00a4eb | 470 | return ops->doit(skb, &info); |
482a8524 TG |
471 | } |
472 | ||
473 | static void genl_rcv(struct sock *sk, int len) | |
474 | { | |
475 | unsigned int qlen = 0; | |
476 | ||
477 | do { | |
478 | if (genl_trylock()) | |
479 | return; | |
e200bd80 | 480 | netlink_run_queue(sk, &qlen, genl_rcv_msg); |
482a8524 TG |
481 | genl_unlock(); |
482 | } while (qlen && genl_sock && genl_sock->sk_receive_queue.qlen); | |
483 | } | |
484 | ||
485 | /************************************************************************** | |
486 | * Controller | |
487 | **************************************************************************/ | |
488 | ||
17c157c8 TG |
489 | static struct genl_family genl_ctrl = { |
490 | .id = GENL_ID_CTRL, | |
491 | .name = "nlctrl", | |
334c29a6 | 492 | .version = 0x2, |
17c157c8 TG |
493 | .maxattr = CTRL_ATTR_MAX, |
494 | }; | |
495 | ||
482a8524 TG |
496 | static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq, |
497 | u32 flags, struct sk_buff *skb, u8 cmd) | |
498 | { | |
499 | void *hdr; | |
500 | ||
17c157c8 | 501 | hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd); |
482a8524 TG |
502 | if (hdr == NULL) |
503 | return -1; | |
504 | ||
505 | NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name); | |
506 | NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id); | |
eb328111 TG |
507 | NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version); |
508 | NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize); | |
509 | NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr); | |
510 | ||
e94ef682 TG |
511 | if (!list_empty(&family->ops_list)) { |
512 | struct nlattr *nla_ops; | |
513 | struct genl_ops *ops; | |
514 | int idx = 1; | |
eb328111 | 515 | |
e94ef682 TG |
516 | nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS); |
517 | if (nla_ops == NULL) | |
eb328111 TG |
518 | goto nla_put_failure; |
519 | ||
e94ef682 TG |
520 | list_for_each_entry(ops, &family->ops_list, ops_list) { |
521 | struct nlattr *nest; | |
eb328111 | 522 | |
e94ef682 TG |
523 | nest = nla_nest_start(skb, idx++); |
524 | if (nest == NULL) | |
525 | goto nla_put_failure; | |
eb328111 | 526 | |
e94ef682 TG |
527 | NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd); |
528 | NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags); | |
eb328111 | 529 | |
e94ef682 TG |
530 | nla_nest_end(skb, nest); |
531 | } | |
532 | ||
533 | nla_nest_end(skb, nla_ops); | |
534 | } | |
482a8524 | 535 | |
2dbba6f7 JB |
536 | if (!list_empty(&family->mcast_groups)) { |
537 | struct genl_multicast_group *grp; | |
538 | struct nlattr *nla_grps; | |
539 | int idx = 1; | |
540 | ||
541 | nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS); | |
542 | if (nla_grps == NULL) | |
543 | goto nla_put_failure; | |
544 | ||
545 | list_for_each_entry(grp, &family->mcast_groups, list) { | |
546 | struct nlattr *nest; | |
547 | ||
548 | nest = nla_nest_start(skb, idx++); | |
549 | if (nest == NULL) | |
550 | goto nla_put_failure; | |
551 | ||
552 | NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id); | |
553 | NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME, | |
554 | grp->name); | |
555 | ||
556 | nla_nest_end(skb, nest); | |
557 | } | |
558 | nla_nest_end(skb, nla_grps); | |
559 | } | |
560 | ||
561 | return genlmsg_end(skb, hdr); | |
562 | ||
563 | nla_put_failure: | |
564 | return genlmsg_cancel(skb, hdr); | |
565 | } | |
566 | ||
567 | static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid, | |
568 | u32 seq, u32 flags, struct sk_buff *skb, | |
569 | u8 cmd) | |
570 | { | |
571 | void *hdr; | |
572 | struct nlattr *nla_grps; | |
573 | struct nlattr *nest; | |
574 | ||
575 | hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd); | |
576 | if (hdr == NULL) | |
577 | return -1; | |
578 | ||
579 | NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name); | |
580 | NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id); | |
581 | ||
582 | nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS); | |
583 | if (nla_grps == NULL) | |
584 | goto nla_put_failure; | |
585 | ||
586 | nest = nla_nest_start(skb, 1); | |
587 | if (nest == NULL) | |
588 | goto nla_put_failure; | |
589 | ||
590 | NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id); | |
591 | NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME, | |
592 | grp->name); | |
593 | ||
594 | nla_nest_end(skb, nest); | |
595 | nla_nest_end(skb, nla_grps); | |
596 | ||
482a8524 TG |
597 | return genlmsg_end(skb, hdr); |
598 | ||
599 | nla_put_failure: | |
600 | return genlmsg_cancel(skb, hdr); | |
601 | } | |
602 | ||
603 | static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb) | |
604 | { | |
605 | ||
606 | int i, n = 0; | |
607 | struct genl_family *rt; | |
608 | int chains_to_skip = cb->args[0]; | |
609 | int fams_to_skip = cb->args[1]; | |
610 | ||
eb328111 TG |
611 | if (chains_to_skip != 0) |
612 | genl_lock(); | |
613 | ||
482a8524 TG |
614 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) { |
615 | if (i < chains_to_skip) | |
616 | continue; | |
617 | n = 0; | |
618 | list_for_each_entry(rt, genl_family_chain(i), family_list) { | |
619 | if (++n < fams_to_skip) | |
620 | continue; | |
621 | if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid, | |
622 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
623 | skb, CTRL_CMD_NEWFAMILY) < 0) | |
624 | goto errout; | |
625 | } | |
626 | ||
627 | fams_to_skip = 0; | |
628 | } | |
629 | ||
630 | errout: | |
eb328111 TG |
631 | if (chains_to_skip != 0) |
632 | genl_unlock(); | |
633 | ||
482a8524 TG |
634 | cb->args[0] = i; |
635 | cb->args[1] = n; | |
636 | ||
637 | return skb->len; | |
638 | } | |
639 | ||
2dbba6f7 JB |
640 | static struct sk_buff *ctrl_build_family_msg(struct genl_family *family, |
641 | u32 pid, int seq, u8 cmd) | |
482a8524 TG |
642 | { |
643 | struct sk_buff *skb; | |
644 | int err; | |
645 | ||
339bf98f | 646 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
482a8524 TG |
647 | if (skb == NULL) |
648 | return ERR_PTR(-ENOBUFS); | |
649 | ||
650 | err = ctrl_fill_info(family, pid, seq, 0, skb, cmd); | |
651 | if (err < 0) { | |
652 | nlmsg_free(skb); | |
653 | return ERR_PTR(err); | |
654 | } | |
655 | ||
656 | return skb; | |
657 | } | |
658 | ||
2dbba6f7 JB |
659 | static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp, |
660 | u32 pid, int seq, u8 cmd) | |
661 | { | |
662 | struct sk_buff *skb; | |
663 | int err; | |
664 | ||
665 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
666 | if (skb == NULL) | |
667 | return ERR_PTR(-ENOBUFS); | |
668 | ||
669 | err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd); | |
670 | if (err < 0) { | |
671 | nlmsg_free(skb); | |
672 | return ERR_PTR(err); | |
673 | } | |
674 | ||
675 | return skb; | |
676 | } | |
677 | ||
ef7c79ed | 678 | static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = { |
482a8524 | 679 | [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 }, |
5176f91e TG |
680 | [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING, |
681 | .len = GENL_NAMSIZ - 1 }, | |
482a8524 TG |
682 | }; |
683 | ||
684 | static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info) | |
685 | { | |
686 | struct sk_buff *msg; | |
687 | struct genl_family *res = NULL; | |
688 | int err = -EINVAL; | |
689 | ||
690 | if (info->attrs[CTRL_ATTR_FAMILY_ID]) { | |
691 | u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]); | |
692 | res = genl_family_find_byid(id); | |
693 | } | |
694 | ||
695 | if (info->attrs[CTRL_ATTR_FAMILY_NAME]) { | |
5176f91e | 696 | char *name; |
482a8524 | 697 | |
5176f91e | 698 | name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]); |
482a8524 TG |
699 | res = genl_family_find_byname(name); |
700 | } | |
701 | ||
702 | if (res == NULL) { | |
703 | err = -ENOENT; | |
704 | goto errout; | |
705 | } | |
706 | ||
2dbba6f7 JB |
707 | msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq, |
708 | CTRL_CMD_NEWFAMILY); | |
482a8524 TG |
709 | if (IS_ERR(msg)) { |
710 | err = PTR_ERR(msg); | |
711 | goto errout; | |
712 | } | |
713 | ||
81878d27 | 714 | err = genlmsg_reply(msg, info); |
482a8524 TG |
715 | errout: |
716 | return err; | |
717 | } | |
718 | ||
719 | static int genl_ctrl_event(int event, void *data) | |
720 | { | |
721 | struct sk_buff *msg; | |
722 | ||
723 | if (genl_sock == NULL) | |
724 | return 0; | |
725 | ||
726 | switch (event) { | |
727 | case CTRL_CMD_NEWFAMILY: | |
728 | case CTRL_CMD_DELFAMILY: | |
2dbba6f7 JB |
729 | msg = ctrl_build_family_msg(data, 0, 0, event); |
730 | if (IS_ERR(msg)) | |
731 | return PTR_ERR(msg); | |
732 | ||
733 | genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL); | |
734 | break; | |
735 | case CTRL_CMD_NEWMCAST_GRP: | |
736 | case CTRL_CMD_DELMCAST_GRP: | |
737 | msg = ctrl_build_mcgrp_msg(data, 0, 0, event); | |
482a8524 TG |
738 | if (IS_ERR(msg)) |
739 | return PTR_ERR(msg); | |
740 | ||
d387f6ad | 741 | genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL); |
482a8524 TG |
742 | break; |
743 | } | |
744 | ||
745 | return 0; | |
746 | } | |
747 | ||
748 | static struct genl_ops genl_ctrl_ops = { | |
749 | .cmd = CTRL_CMD_GETFAMILY, | |
750 | .doit = ctrl_getfamily, | |
751 | .dumpit = ctrl_dumpfamily, | |
752 | .policy = ctrl_policy, | |
753 | }; | |
754 | ||
2dbba6f7 JB |
755 | static struct genl_multicast_group notify_grp = { |
756 | .name = "notify", | |
757 | }; | |
758 | ||
482a8524 TG |
759 | static int __init genl_init(void) |
760 | { | |
761 | int i, err; | |
762 | ||
763 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) | |
764 | INIT_LIST_HEAD(&family_ht[i]); | |
765 | ||
766 | err = genl_register_family(&genl_ctrl); | |
767 | if (err < 0) | |
768 | goto errout; | |
769 | ||
770 | err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops); | |
771 | if (err < 0) | |
772 | goto errout_register; | |
773 | ||
774 | netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV); | |
2dbba6f7 JB |
775 | |
776 | /* we'll bump the group number right afterwards */ | |
777 | genl_sock = netlink_kernel_create(NETLINK_GENERIC, 0, genl_rcv, | |
778 | NULL, THIS_MODULE); | |
e200bd80 | 779 | if (genl_sock == NULL) |
482a8524 | 780 | panic("GENL: Cannot initialize generic netlink\n"); |
482a8524 | 781 | |
2dbba6f7 JB |
782 | err = genl_register_mc_group(&genl_ctrl, ¬ify_grp); |
783 | if (err < 0) | |
784 | goto errout_register; | |
785 | ||
482a8524 TG |
786 | return 0; |
787 | ||
788 | errout_register: | |
789 | genl_unregister_family(&genl_ctrl); | |
790 | errout: | |
791 | panic("GENL: Cannot register controller: %d\n", err); | |
482a8524 TG |
792 | } |
793 | ||
794 | subsys_initcall(genl_init); | |
795 | ||
796 | EXPORT_SYMBOL(genl_sock); | |
797 | EXPORT_SYMBOL(genl_register_ops); | |
798 | EXPORT_SYMBOL(genl_unregister_ops); | |
799 | EXPORT_SYMBOL(genl_register_family); | |
800 | EXPORT_SYMBOL(genl_unregister_family); |