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