2 * NETLINK Generic Netlink Family
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
6 * Johannes Berg <johannes@sipsolutions.net>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.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 <linux/mutex.h>
18 #include <linux/bitmap.h>
19 #include <linux/rwsem.h>
21 #include <net/genetlink.h>
23 static DEFINE_MUTEX(genl_mutex
); /* serialization of message processing */
24 static DECLARE_RWSEM(cb_lock
);
28 mutex_lock(&genl_mutex
);
30 EXPORT_SYMBOL(genl_lock
);
32 void genl_unlock(void)
34 mutex_unlock(&genl_mutex
);
36 EXPORT_SYMBOL(genl_unlock
);
39 int lockdep_genl_is_held(void)
41 return lockdep_is_held(&genl_mutex
);
43 EXPORT_SYMBOL(lockdep_genl_is_held
);
46 static void genl_lock_all(void)
52 static void genl_unlock_all(void)
58 #define GENL_FAM_TAB_SIZE 16
59 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
61 static struct list_head family_ht
[GENL_FAM_TAB_SIZE
];
63 * Bitmap of multicast groups that are currently in use.
65 * To avoid an allocation at boot of just one unsigned long,
66 * declare it global instead.
67 * Bit 0 is marked as already used since group 0 is invalid.
69 static unsigned long mc_group_start
= 0x1;
70 static unsigned long *mc_groups
= &mc_group_start
;
71 static unsigned long mc_groups_longs
= 1;
73 static int genl_ctrl_event(int event
, void *data
);
75 static inline unsigned int genl_family_hash(unsigned int id
)
77 return id
& GENL_FAM_TAB_MASK
;
80 static inline struct list_head
*genl_family_chain(unsigned int id
)
82 return &family_ht
[genl_family_hash(id
)];
85 static struct genl_family
*genl_family_find_byid(unsigned int id
)
87 struct genl_family
*f
;
89 list_for_each_entry(f
, genl_family_chain(id
), family_list
)
96 static struct genl_family
*genl_family_find_byname(char *name
)
98 struct genl_family
*f
;
101 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
102 list_for_each_entry(f
, genl_family_chain(i
), family_list
)
103 if (strcmp(f
->name
, name
) == 0)
109 static struct genl_ops
*genl_get_cmd(u8 cmd
, struct genl_family
*family
)
111 struct genl_ops
*ops
;
113 list_for_each_entry(ops
, &family
->ops_list
, ops_list
)
120 /* Of course we are going to have problems once we hit
121 * 2^16 alive types, but that can only happen by year 2K
123 static u16
genl_generate_id(void)
125 static u16 id_gen_idx
= GENL_MIN_ID
;
128 for (i
= 0; i
<= GENL_MAX_ID
- GENL_MIN_ID
; i
++) {
129 if (!genl_family_find_byid(id_gen_idx
))
131 if (++id_gen_idx
> GENL_MAX_ID
)
132 id_gen_idx
= GENL_MIN_ID
;
138 static struct genl_multicast_group notify_grp
;
141 * genl_register_mc_group - register a multicast group
143 * Registers the specified multicast group and notifies userspace
144 * about the new group.
146 * Returns 0 on success or a negative error code.
148 * @family: The generic netlink family the group shall be registered for.
149 * @grp: The group to register, must have a name.
151 int genl_register_mc_group(struct genl_family
*family
,
152 struct genl_multicast_group
*grp
)
155 unsigned long *new_groups
;
158 BUG_ON(grp
->name
[0] == '\0');
159 BUG_ON(memchr(grp
->name
, '\0', GENL_NAMSIZ
) == NULL
);
163 /* special-case our own group */
164 if (grp
== ¬ify_grp
)
167 id
= find_first_zero_bit(mc_groups
,
168 mc_groups_longs
* BITS_PER_LONG
);
171 if (id
>= mc_groups_longs
* BITS_PER_LONG
) {
172 size_t nlen
= (mc_groups_longs
+ 1) * sizeof(unsigned long);
174 if (mc_groups
== &mc_group_start
) {
175 new_groups
= kzalloc(nlen
, GFP_KERNEL
);
180 mc_groups
= new_groups
;
181 *mc_groups
= mc_group_start
;
183 new_groups
= krealloc(mc_groups
, nlen
, GFP_KERNEL
);
188 mc_groups
= new_groups
;
189 mc_groups
[mc_groups_longs
] = 0;
194 if (family
->netnsok
) {
197 netlink_table_grab();
199 for_each_net_rcu(net
) {
200 err
= __netlink_change_ngroups(net
->genl_sock
,
201 mc_groups_longs
* BITS_PER_LONG
);
204 * No need to roll back, can only fail if
205 * memory allocation fails and then the
206 * number of _possible_ groups has been
207 * increased on some sockets which is ok.
210 netlink_table_ungrab();
215 netlink_table_ungrab();
217 err
= netlink_change_ngroups(init_net
.genl_sock
,
218 mc_groups_longs
* BITS_PER_LONG
);
224 set_bit(id
, mc_groups
);
225 list_add_tail(&grp
->list
, &family
->mcast_groups
);
226 grp
->family
= family
;
228 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP
, grp
);
233 EXPORT_SYMBOL(genl_register_mc_group
);
235 static void __genl_unregister_mc_group(struct genl_family
*family
,
236 struct genl_multicast_group
*grp
)
239 BUG_ON(grp
->family
!= family
);
241 netlink_table_grab();
243 for_each_net_rcu(net
)
244 __netlink_clear_multicast_users(net
->genl_sock
, grp
->id
);
246 netlink_table_ungrab();
248 clear_bit(grp
->id
, mc_groups
);
249 list_del(&grp
->list
);
250 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP
, grp
);
256 * genl_unregister_mc_group - unregister a multicast group
258 * Unregisters the specified multicast group and notifies userspace
259 * about it. All current listeners on the group are removed.
261 * Note: It is not necessary to unregister all multicast groups before
262 * unregistering the family, unregistering the family will cause
263 * all assigned multicast groups to be unregistered automatically.
265 * @family: Generic netlink family the group belongs to.
266 * @grp: The group to unregister, must have been registered successfully
269 void genl_unregister_mc_group(struct genl_family
*family
,
270 struct genl_multicast_group
*grp
)
273 __genl_unregister_mc_group(family
, grp
);
276 EXPORT_SYMBOL(genl_unregister_mc_group
);
278 static void genl_unregister_mc_groups(struct genl_family
*family
)
280 struct genl_multicast_group
*grp
, *tmp
;
282 list_for_each_entry_safe(grp
, tmp
, &family
->mcast_groups
, list
)
283 __genl_unregister_mc_group(family
, grp
);
287 * genl_register_ops - register generic netlink operations
288 * @family: generic netlink family
289 * @ops: operations to be registered
291 * Registers the specified operations and assigns them to the specified
292 * family. Either a doit or dumpit callback must be specified or the
293 * operation will fail. Only one operation structure per command
294 * identifier may be registered.
296 * See include/net/genetlink.h for more documenation on the operations
299 * Returns 0 on success or a negative error code.
301 int genl_register_ops(struct genl_family
*family
, struct genl_ops
*ops
)
305 if (ops
->dumpit
== NULL
&& ops
->doit
== NULL
)
308 if (genl_get_cmd(ops
->cmd
, family
)) {
314 ops
->flags
|= GENL_CMD_CAP_DUMP
;
316 ops
->flags
|= GENL_CMD_CAP_DO
;
318 ops
->flags
|= GENL_CMD_CAP_HASPOL
;
321 list_add_tail(&ops
->ops_list
, &family
->ops_list
);
324 genl_ctrl_event(CTRL_CMD_NEWOPS
, ops
);
329 EXPORT_SYMBOL(genl_register_ops
);
332 * genl_unregister_ops - unregister generic netlink operations
333 * @family: generic netlink family
334 * @ops: operations to be unregistered
336 * Unregisters the specified operations and unassigns them from the
337 * specified family. The operation blocks until the current message
338 * processing has finished and doesn't start again until the
339 * unregister process has finished.
341 * Note: It is not necessary to unregister all operations before
342 * unregistering the family, unregistering the family will cause
343 * all assigned operations to be unregistered automatically.
345 * Returns 0 on success or a negative error code.
347 int genl_unregister_ops(struct genl_family
*family
, struct genl_ops
*ops
)
352 list_for_each_entry(rc
, &family
->ops_list
, ops_list
) {
354 list_del(&ops
->ops_list
);
356 genl_ctrl_event(CTRL_CMD_DELOPS
, ops
);
364 EXPORT_SYMBOL(genl_unregister_ops
);
367 * genl_register_family - register a generic netlink family
368 * @family: generic netlink family
370 * Registers the specified family after validating it first. Only one
371 * family may be registered with the same family name or identifier.
372 * The family id may equal GENL_ID_GENERATE causing an unique id to
373 * be automatically generated and assigned.
375 * Return 0 on success or a negative error code.
377 int genl_register_family(struct genl_family
*family
)
381 if (family
->id
&& family
->id
< GENL_MIN_ID
)
384 if (family
->id
> GENL_MAX_ID
)
387 INIT_LIST_HEAD(&family
->ops_list
);
388 INIT_LIST_HEAD(&family
->mcast_groups
);
392 if (genl_family_find_byname(family
->name
)) {
397 if (family
->id
== GENL_ID_GENERATE
) {
398 u16 newid
= genl_generate_id();
406 } else if (genl_family_find_byid(family
->id
)) {
411 if (family
->maxattr
&& !family
->parallel_ops
) {
412 family
->attrbuf
= kmalloc((family
->maxattr
+1) *
413 sizeof(struct nlattr
*), GFP_KERNEL
);
414 if (family
->attrbuf
== NULL
) {
419 family
->attrbuf
= NULL
;
421 list_add_tail(&family
->family_list
, genl_family_chain(family
->id
));
424 genl_ctrl_event(CTRL_CMD_NEWFAMILY
, family
);
433 EXPORT_SYMBOL(genl_register_family
);
436 * genl_register_family_with_ops - register a generic netlink family
437 * @family: generic netlink family
438 * @ops: operations to be registered
439 * @n_ops: number of elements to register
441 * Registers the specified family and operations from the specified table.
442 * Only one family may be registered with the same family name or identifier.
444 * The family id may equal GENL_ID_GENERATE causing an unique id to
445 * be automatically generated and assigned.
447 * Either a doit or dumpit callback must be specified for every registered
448 * operation or the function will fail. Only one operation structure per
449 * command identifier may be registered.
451 * See include/net/genetlink.h for more documenation on the operations
454 * This is equivalent to calling genl_register_family() followed by
455 * genl_register_ops() for every operation entry in the table taking
456 * care to unregister the family on error path.
458 * Return 0 on success or a negative error code.
460 int genl_register_family_with_ops(struct genl_family
*family
,
461 struct genl_ops
*ops
, size_t n_ops
)
465 err
= genl_register_family(family
);
469 for (i
= 0; i
< n_ops
; ++i
, ++ops
) {
470 err
= genl_register_ops(family
, ops
);
476 genl_unregister_family(family
);
479 EXPORT_SYMBOL(genl_register_family_with_ops
);
482 * genl_unregister_family - unregister generic netlink family
483 * @family: generic netlink family
485 * Unregisters the specified family.
487 * Returns 0 on success or a negative error code.
489 int genl_unregister_family(struct genl_family
*family
)
491 struct genl_family
*rc
;
495 genl_unregister_mc_groups(family
);
497 list_for_each_entry(rc
, genl_family_chain(family
->id
), family_list
) {
498 if (family
->id
!= rc
->id
|| strcmp(rc
->name
, family
->name
))
501 list_del(&rc
->family_list
);
502 INIT_LIST_HEAD(&family
->ops_list
);
505 kfree(family
->attrbuf
);
506 genl_ctrl_event(CTRL_CMD_DELFAMILY
, family
);
514 EXPORT_SYMBOL(genl_unregister_family
);
517 * genlmsg_put - Add generic netlink header to netlink message
518 * @skb: socket buffer holding the message
519 * @portid: netlink portid the message is addressed to
520 * @seq: sequence number (usually the one of the sender)
521 * @family: generic netlink family
522 * @flags: netlink message flags
523 * @cmd: generic netlink command
525 * Returns pointer to user specific header
527 void *genlmsg_put(struct sk_buff
*skb
, u32 portid
, u32 seq
,
528 struct genl_family
*family
, int flags
, u8 cmd
)
530 struct nlmsghdr
*nlh
;
531 struct genlmsghdr
*hdr
;
533 nlh
= nlmsg_put(skb
, portid
, seq
, family
->id
, GENL_HDRLEN
+
534 family
->hdrsize
, flags
);
538 hdr
= nlmsg_data(nlh
);
540 hdr
->version
= family
->version
;
543 return (char *) hdr
+ GENL_HDRLEN
;
545 EXPORT_SYMBOL(genlmsg_put
);
547 static int genl_family_rcv_msg(struct genl_family
*family
,
549 struct nlmsghdr
*nlh
)
551 struct genl_ops
*ops
;
552 struct net
*net
= sock_net(skb
->sk
);
553 struct genl_info info
;
554 struct genlmsghdr
*hdr
= nlmsg_data(nlh
);
555 struct nlattr
**attrbuf
;
558 /* this family doesn't exist in this netns */
559 if (!family
->netnsok
&& !net_eq(net
, &init_net
))
562 hdrlen
= GENL_HDRLEN
+ family
->hdrsize
;
563 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
566 ops
= genl_get_cmd(hdr
->cmd
, family
);
570 if ((ops
->flags
& GENL_ADMIN_PERM
) &&
571 !capable(CAP_NET_ADMIN
))
574 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
575 struct netlink_dump_control c
= {
580 if (ops
->dumpit
== NULL
)
583 return netlink_dump_start(net
->genl_sock
, skb
, nlh
, &c
);
586 if (ops
->doit
== NULL
)
589 if (family
->maxattr
&& family
->parallel_ops
) {
590 attrbuf
= kmalloc((family
->maxattr
+1) *
591 sizeof(struct nlattr
*), GFP_KERNEL
);
595 attrbuf
= family
->attrbuf
;
598 err
= nlmsg_parse(nlh
, hdrlen
, attrbuf
, family
->maxattr
,
604 info
.snd_seq
= nlh
->nlmsg_seq
;
605 info
.snd_portid
= NETLINK_CB(skb
).portid
;
607 info
.genlhdr
= nlmsg_data(nlh
);
608 info
.userhdr
= nlmsg_data(nlh
) + GENL_HDRLEN
;
609 info
.attrs
= attrbuf
;
610 genl_info_net_set(&info
, net
);
611 memset(&info
.user_ptr
, 0, sizeof(info
.user_ptr
));
613 if (family
->pre_doit
) {
614 err
= family
->pre_doit(ops
, skb
, &info
);
619 err
= ops
->doit(skb
, &info
);
621 if (family
->post_doit
)
622 family
->post_doit(ops
, skb
, &info
);
625 if (family
->parallel_ops
)
631 static int genl_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
633 struct genl_family
*family
;
636 family
= genl_family_find_byid(nlh
->nlmsg_type
);
640 if (!family
->parallel_ops
)
643 err
= genl_family_rcv_msg(family
, skb
, nlh
);
645 if (!family
->parallel_ops
)
651 static void genl_rcv(struct sk_buff
*skb
)
654 netlink_rcv_skb(skb
, &genl_rcv_msg
);
658 /**************************************************************************
660 **************************************************************************/
662 static struct genl_family genl_ctrl
= {
666 .maxattr
= CTRL_ATTR_MAX
,
670 static int ctrl_fill_info(struct genl_family
*family
, u32 portid
, u32 seq
,
671 u32 flags
, struct sk_buff
*skb
, u8 cmd
)
675 hdr
= genlmsg_put(skb
, portid
, seq
, &genl_ctrl
, flags
, cmd
);
679 if (nla_put_string(skb
, CTRL_ATTR_FAMILY_NAME
, family
->name
) ||
680 nla_put_u16(skb
, CTRL_ATTR_FAMILY_ID
, family
->id
) ||
681 nla_put_u32(skb
, CTRL_ATTR_VERSION
, family
->version
) ||
682 nla_put_u32(skb
, CTRL_ATTR_HDRSIZE
, family
->hdrsize
) ||
683 nla_put_u32(skb
, CTRL_ATTR_MAXATTR
, family
->maxattr
))
684 goto nla_put_failure
;
686 if (!list_empty(&family
->ops_list
)) {
687 struct nlattr
*nla_ops
;
688 struct genl_ops
*ops
;
691 nla_ops
= nla_nest_start(skb
, CTRL_ATTR_OPS
);
693 goto nla_put_failure
;
695 list_for_each_entry(ops
, &family
->ops_list
, ops_list
) {
698 nest
= nla_nest_start(skb
, idx
++);
700 goto nla_put_failure
;
702 if (nla_put_u32(skb
, CTRL_ATTR_OP_ID
, ops
->cmd
) ||
703 nla_put_u32(skb
, CTRL_ATTR_OP_FLAGS
, ops
->flags
))
704 goto nla_put_failure
;
706 nla_nest_end(skb
, nest
);
709 nla_nest_end(skb
, nla_ops
);
712 if (!list_empty(&family
->mcast_groups
)) {
713 struct genl_multicast_group
*grp
;
714 struct nlattr
*nla_grps
;
717 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
718 if (nla_grps
== NULL
)
719 goto nla_put_failure
;
721 list_for_each_entry(grp
, &family
->mcast_groups
, list
) {
724 nest
= nla_nest_start(skb
, idx
++);
726 goto nla_put_failure
;
728 if (nla_put_u32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
) ||
729 nla_put_string(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
731 goto nla_put_failure
;
733 nla_nest_end(skb
, nest
);
735 nla_nest_end(skb
, nla_grps
);
738 return genlmsg_end(skb
, hdr
);
741 genlmsg_cancel(skb
, hdr
);
745 static int ctrl_fill_mcgrp_info(struct genl_multicast_group
*grp
, u32 portid
,
746 u32 seq
, u32 flags
, struct sk_buff
*skb
,
750 struct nlattr
*nla_grps
;
753 hdr
= genlmsg_put(skb
, portid
, seq
, &genl_ctrl
, flags
, cmd
);
757 if (nla_put_string(skb
, CTRL_ATTR_FAMILY_NAME
, grp
->family
->name
) ||
758 nla_put_u16(skb
, CTRL_ATTR_FAMILY_ID
, grp
->family
->id
))
759 goto nla_put_failure
;
761 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
762 if (nla_grps
== NULL
)
763 goto nla_put_failure
;
765 nest
= nla_nest_start(skb
, 1);
767 goto nla_put_failure
;
769 if (nla_put_u32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
) ||
770 nla_put_string(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
772 goto nla_put_failure
;
774 nla_nest_end(skb
, nest
);
775 nla_nest_end(skb
, nla_grps
);
777 return genlmsg_end(skb
, hdr
);
780 genlmsg_cancel(skb
, hdr
);
784 static int ctrl_dumpfamily(struct sk_buff
*skb
, struct netlink_callback
*cb
)
788 struct genl_family
*rt
;
789 struct net
*net
= sock_net(skb
->sk
);
790 int chains_to_skip
= cb
->args
[0];
791 int fams_to_skip
= cb
->args
[1];
793 for (i
= chains_to_skip
; i
< GENL_FAM_TAB_SIZE
; i
++) {
795 list_for_each_entry(rt
, genl_family_chain(i
), family_list
) {
796 if (!rt
->netnsok
&& !net_eq(net
, &init_net
))
798 if (++n
< fams_to_skip
)
800 if (ctrl_fill_info(rt
, NETLINK_CB(cb
->skb
).portid
,
801 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
802 skb
, CTRL_CMD_NEWFAMILY
) < 0)
816 static struct sk_buff
*ctrl_build_family_msg(struct genl_family
*family
,
817 u32 portid
, int seq
, u8 cmd
)
822 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
824 return ERR_PTR(-ENOBUFS
);
826 err
= ctrl_fill_info(family
, portid
, seq
, 0, skb
, cmd
);
835 static struct sk_buff
*ctrl_build_mcgrp_msg(struct genl_multicast_group
*grp
,
836 u32 portid
, int seq
, u8 cmd
)
841 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
843 return ERR_PTR(-ENOBUFS
);
845 err
= ctrl_fill_mcgrp_info(grp
, portid
, seq
, 0, skb
, cmd
);
854 static const struct nla_policy ctrl_policy
[CTRL_ATTR_MAX
+1] = {
855 [CTRL_ATTR_FAMILY_ID
] = { .type
= NLA_U16
},
856 [CTRL_ATTR_FAMILY_NAME
] = { .type
= NLA_NUL_STRING
,
857 .len
= GENL_NAMSIZ
- 1 },
860 static int ctrl_getfamily(struct sk_buff
*skb
, struct genl_info
*info
)
863 struct genl_family
*res
= NULL
;
866 if (info
->attrs
[CTRL_ATTR_FAMILY_ID
]) {
867 u16 id
= nla_get_u16(info
->attrs
[CTRL_ATTR_FAMILY_ID
]);
868 res
= genl_family_find_byid(id
);
872 if (info
->attrs
[CTRL_ATTR_FAMILY_NAME
]) {
875 name
= nla_data(info
->attrs
[CTRL_ATTR_FAMILY_NAME
]);
876 res
= genl_family_find_byname(name
);
877 #ifdef CONFIG_MODULES
880 request_module("net-pf-%d-proto-%d-family-%s",
881 PF_NETLINK
, NETLINK_GENERIC
, name
);
883 res
= genl_family_find_byname(name
);
892 if (!res
->netnsok
&& !net_eq(genl_info_net(info
), &init_net
)) {
893 /* family doesn't exist here */
897 msg
= ctrl_build_family_msg(res
, info
->snd_portid
, info
->snd_seq
,
902 return genlmsg_reply(msg
, info
);
905 static int genl_ctrl_event(int event
, void *data
)
908 struct genl_family
*family
;
909 struct genl_multicast_group
*grp
;
911 /* genl is still initialising */
912 if (!init_net
.genl_sock
)
916 case CTRL_CMD_NEWFAMILY
:
917 case CTRL_CMD_DELFAMILY
:
919 msg
= ctrl_build_family_msg(family
, 0, 0, event
);
921 case CTRL_CMD_NEWMCAST_GRP
:
922 case CTRL_CMD_DELMCAST_GRP
:
924 family
= grp
->family
;
925 msg
= ctrl_build_mcgrp_msg(data
, 0, 0, event
);
934 if (!family
->netnsok
) {
935 genlmsg_multicast_netns(&init_net
, msg
, 0,
936 GENL_ID_CTRL
, GFP_KERNEL
);
939 genlmsg_multicast_allns(msg
, 0, GENL_ID_CTRL
, GFP_ATOMIC
);
946 static struct genl_ops genl_ctrl_ops
= {
947 .cmd
= CTRL_CMD_GETFAMILY
,
948 .doit
= ctrl_getfamily
,
949 .dumpit
= ctrl_dumpfamily
,
950 .policy
= ctrl_policy
,
953 static struct genl_multicast_group notify_grp
= {
957 static int __net_init
genl_pernet_init(struct net
*net
)
959 struct netlink_kernel_cfg cfg
= {
961 .flags
= NL_CFG_F_NONROOT_RECV
,
964 /* we'll bump the group number right afterwards */
965 net
->genl_sock
= netlink_kernel_create(net
, NETLINK_GENERIC
, &cfg
);
967 if (!net
->genl_sock
&& net_eq(net
, &init_net
))
968 panic("GENL: Cannot initialize generic netlink\n");
976 static void __net_exit
genl_pernet_exit(struct net
*net
)
978 netlink_kernel_release(net
->genl_sock
);
979 net
->genl_sock
= NULL
;
982 static struct pernet_operations genl_pernet_ops
= {
983 .init
= genl_pernet_init
,
984 .exit
= genl_pernet_exit
,
987 static int __init
genl_init(void)
991 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
992 INIT_LIST_HEAD(&family_ht
[i
]);
994 err
= genl_register_family_with_ops(&genl_ctrl
, &genl_ctrl_ops
, 1);
998 err
= register_pernet_subsys(&genl_pernet_ops
);
1002 err
= genl_register_mc_group(&genl_ctrl
, ¬ify_grp
);
1009 panic("GENL: Cannot register controller: %d\n", err
);
1012 subsys_initcall(genl_init
);
1014 static int genlmsg_mcast(struct sk_buff
*skb
, u32 portid
, unsigned long group
,
1017 struct sk_buff
*tmp
;
1018 struct net
*net
, *prev
= NULL
;
1021 for_each_net_rcu(net
) {
1023 tmp
= skb_clone(skb
, flags
);
1028 err
= nlmsg_multicast(prev
->genl_sock
, tmp
,
1029 portid
, group
, flags
);
1037 return nlmsg_multicast(prev
->genl_sock
, skb
, portid
, group
, flags
);
1043 int genlmsg_multicast_allns(struct sk_buff
*skb
, u32 portid
, unsigned int group
,
1046 return genlmsg_mcast(skb
, portid
, group
, flags
);
1048 EXPORT_SYMBOL(genlmsg_multicast_allns
);
1050 void genl_notify(struct sk_buff
*skb
, struct net
*net
, u32 portid
, u32 group
,
1051 struct nlmsghdr
*nlh
, gfp_t flags
)
1053 struct sock
*sk
= net
->genl_sock
;
1057 report
= nlmsg_report(nlh
);
1059 nlmsg_notify(sk
, skb
, portid
, group
, report
, flags
);
1061 EXPORT_SYMBOL(genl_notify
);