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