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