History

genetlink: use idr to track families

Since generic netlink family IDs are small integers, allocated densely, IDR is an ideal match for lookups. Replace the existing hand-written hash-table with IDR for allocation and lookup. This lets the families only be written to once, during register, since the list_head can be removed and removal of a family won't cause any writes. It also slightly reduces the code size (by about 1.3k on x86-64).

Johannes Berg committed 10 years ago

Commit 2ae0f171 parent 489111e3 files · +123 −181Browse files

Unified · Split

3 files
  1. include/net/genetlink.h +15 −16
  2. include/uapi/linux/genetlink.h +2 −0
  3. net/netlink/genetlink.c +106 −165
include/net/genetlink.h+15−16
@@ -40,7 +40,6 @@ struct genl_info; · 39 unmodified lines
40 * generic netlink family is removed while there are still open40 * generic netlink family is removed while there are still open
41 * sockets.41 * sockets.
42 * @attrbuf: buffer to store parsed attributes (private)42 * @attrbuf: buffer to store parsed attributes (private)
43 * @family_list: family list (private)
44 * @mcgrps: multicast groups used by this family43 * @mcgrps: multicast groups used by this family
45 * @n_mcgrps: number of multicast groups44 * @n_mcgrps: number of multicast groups
46 * @mcgrp_offset: starting number of multicast group IDs in this family45 * @mcgrp_offset: starting number of multicast group IDs in this family
@@ -70,11 +69,10 @@ struct genl_family { · 23 unmodified lines
70 unsigned int n_ops;69 unsigned int n_ops;
71 unsigned int n_mcgrps;70 unsigned int n_mcgrps;
72 unsigned int mcgrp_offset; /* private */71 unsigned int mcgrp_offset; /* private */
73 struct list_head family_list; /* private */
74 struct module *module;72 struct module *module;
75};73};
76 74
77struct nlattr **genl_family_attrbuf(struct genl_family *family);75struct nlattr **genl_family_attrbuf(const struct genl_family *family);
78 76
79/**77/**
80 * struct genl_info - receiving information78 * struct genl_info - receiving information
@@ -134,12 +132,12 @@ struct genl_ops { · 53 unmodified lines
134};132};
135 133
136int genl_register_family(struct genl_family *family);134int genl_register_family(struct genl_family *family);
137int genl_unregister_family(struct genl_family *family);135int genl_unregister_family(const struct genl_family *family);
138void genl_notify(struct genl_family *family, struct sk_buff *skb,136void genl_notify(const struct genl_family *family, struct sk_buff *skb,
139 struct genl_info *info, u32 group, gfp_t flags);137 struct genl_info *info, u32 group, gfp_t flags);
140 138
141void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,139void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
142 struct genl_family *family, int flags, u8 cmd);140 const struct genl_family *family, int flags, u8 cmd);
143 141
144/**142/**
145 * genlmsg_nlhdr - Obtain netlink header from user specified header143 * genlmsg_nlhdr - Obtain netlink header from user specified header
@@ -148,8 +146,8 @@ void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, · 2 unmodified lines
148 *146 *
149 * Returns pointer to netlink header.147 * Returns pointer to netlink header.
150 */148 */
151static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr,149static inline struct nlmsghdr *
152 struct genl_family *family)150genlmsg_nlhdr(void *user_hdr, const struct genl_family *family)
153{151{
154 return (struct nlmsghdr *)((char *)user_hdr -152 return (struct nlmsghdr *)((char *)user_hdr -
155 family->hdrsize -153 family->hdrsize -
@@ -185,7 +183,7 @@ static inline int genlmsg_parse(const struct nlmsghdr *nlh, · 29 unmodified lines
185 */183 */
186static inline void genl_dump_check_consistent(struct netlink_callback *cb,184static inline void genl_dump_check_consistent(struct netlink_callback *cb,
187 void *user_hdr,185 void *user_hdr,
188 struct genl_family *family)186 const struct genl_family *family)
189{187{
190 nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr, family));188 nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr, family));
191}189}
@@ -202,7 +200,7 @@ static inline void genl_dump_check_consistent(struct netlink_callback *cb, · 10 unmodified lines
202 */200 */
203static inline void *genlmsg_put_reply(struct sk_buff *skb,201static inline void *genlmsg_put_reply(struct sk_buff *skb,
204 struct genl_info *info,202 struct genl_info *info,
205 struct genl_family *family,203 const struct genl_family *family,
206 int flags, u8 cmd)204 int flags, u8 cmd)
207{205{
208 return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,206 return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
@@ -239,7 +237,7 @@ static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr) · 30 unmodified lines
239 * @group: offset of multicast group in groups array237 * @group: offset of multicast group in groups array
240 * @flags: allocation flags238 * @flags: allocation flags
241 */239 */
242static inline int genlmsg_multicast_netns(struct genl_family *family,240static inline int genlmsg_multicast_netns(const struct genl_family *family,
243 struct net *net, struct sk_buff *skb,241 struct net *net, struct sk_buff *skb,
244 u32 portid, unsigned int group, gfp_t flags)242 u32 portid, unsigned int group, gfp_t flags)
245{243{
@@ -257,7 +255,7 @@ static inline int genlmsg_multicast_netns(struct genl_family *family, · 11 unmodified lines
257 * @group: offset of multicast group in groups array255 * @group: offset of multicast group in groups array
258 * @flags: allocation flags256 * @flags: allocation flags
259 */257 */
260static inline int genlmsg_multicast(struct genl_family *family,258static inline int genlmsg_multicast(const struct genl_family *family,
261 struct sk_buff *skb, u32 portid,259 struct sk_buff *skb, u32 portid,
262 unsigned int group, gfp_t flags)260 unsigned int group, gfp_t flags)
263{261{
@@ -275,7 +273,7 @@ static inline int genlmsg_multicast(struct genl_family *family, · 11 unmodified lines
275 *273 *
276 * This function must hold the RTNL or rcu_read_lock().274 * This function must hold the RTNL or rcu_read_lock().
277 */275 */
278int genlmsg_multicast_allns(struct genl_family *family,276int genlmsg_multicast_allns(const struct genl_family *family,
279 struct sk_buff *skb, u32 portid,277 struct sk_buff *skb, u32 portid,
280 unsigned int group, gfp_t flags);278 unsigned int group, gfp_t flags);
281 279
@@ -359,8 +357,9 @@ static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags) · 77 unmodified lines
359 * This function returns the number of broadcast listeners that have set the357 * This function returns the number of broadcast listeners that have set the
360 * NETLINK_RECV_NO_ENOBUFS socket option.358 * NETLINK_RECV_NO_ENOBUFS socket option.
361 */359 */
362static inline int genl_set_err(struct genl_family *family, struct net *net,360static inline int genl_set_err(const struct genl_family *family,
363 u32 portid, u32 group, int code)361 struct net *net, u32 portid,
362 u32 group, int code)
364{363{
365 if (WARN_ON_ONCE(group >= family->n_mcgrps))364 if (WARN_ON_ONCE(group >= family->n_mcgrps))
366 return -EINVAL;365 return -EINVAL;
@@ -368,7 +367,7 @@ static inline int genl_set_err(struct genl_family *family, struct net *net, · 1 unmodified line
368 return netlink_set_err(net->genl_sock, portid, group, code);367 return netlink_set_err(net->genl_sock, portid, group, code);
369}368}
370 369
371static inline int genl_has_listeners(struct genl_family *family,370static inline int genl_has_listeners(const struct genl_family *family,
372 struct net *net, unsigned int group)371 struct net *net, unsigned int group)
373{372{
374 if (WARN_ON_ONCE(group >= family->n_mcgrps))373 if (WARN_ON_ONCE(group >= family->n_mcgrps))
include/uapi/linux/genetlink.h+2−0
@@ -29,6 +29,8 @@ struct genlmsghdr { · 28 unmodified lines
29#define GENL_ID_CTRL NLMSG_MIN_TYPE29#define GENL_ID_CTRL NLMSG_MIN_TYPE
30#define GENL_ID_VFS_DQUOT (NLMSG_MIN_TYPE + 1)30#define GENL_ID_VFS_DQUOT (NLMSG_MIN_TYPE + 1)
31#define GENL_ID_PMCRAID (NLMSG_MIN_TYPE + 2)31#define GENL_ID_PMCRAID (NLMSG_MIN_TYPE + 2)
32/* must be last reserved + 1 */
33#define GENL_START_ALLOC (NLMSG_MIN_TYPE + 3)
32 34
33/**************************************************************************35/**************************************************************************
34 * Controller36 * Controller
net/netlink/genetlink.c+106−165
@@ -17,6 +17,7 @@ · 16 unmodified lines
17#include <linux/mutex.h>17#include <linux/mutex.h>
18#include <linux/bitmap.h>18#include <linux/bitmap.h>
19#include <linux/rwsem.h>19#include <linux/rwsem.h>
20#include <linux/idr.h>
20#include <net/sock.h>21#include <net/sock.h>
21#include <net/genetlink.h>22#include <net/genetlink.h>
22 23
@@ -58,10 +59,8 @@ static void genl_unlock_all(void) · 35 unmodified lines
58 up_write(&cb_lock);59 up_write(&cb_lock);
59}60}
60 61
61#define GENL_FAM_TAB_SIZE 1662static DEFINE_IDR(genl_fam_idr);
62#define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
63 63
64static struct list_head family_ht[GENL_FAM_TAB_SIZE];
65/*64/*
66 * Bitmap of multicast groups that are currently in use.65 * Bitmap of multicast groups that are currently in use.
67 *66 *
@@ -86,45 +85,29 @@ static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_CTRL) | · 18 unmodified lines
86static unsigned long *mc_groups = &mc_group_start;85static unsigned long *mc_groups = &mc_group_start;
87static unsigned long mc_groups_longs = 1;86static unsigned long mc_groups_longs = 1;
88 87
89static int genl_ctrl_event(int event, struct genl_family *family,88static int genl_ctrl_event(int event, const struct genl_family *family,
90 const struct genl_multicast_group *grp,89 const struct genl_multicast_group *grp,
91 int grp_id);90 int grp_id);
92 91
93static inline unsigned int genl_family_hash(unsigned int id)92static const struct genl_family *genl_family_find_byid(unsigned int id)
94{93{
95 return id & GENL_FAM_TAB_MASK;94 return idr_find(&genl_fam_idr, id);
96}95}
97 96
98static inline struct list_head *genl_family_chain(unsigned int id)97static const struct genl_family *genl_family_find_byname(char *name)
99{98{
100 return &family_ht[genl_family_hash(id)];99 const struct genl_family *family;
101}100 unsigned int id;
102
103static struct genl_family *genl_family_find_byid(unsigned int id)
104{
105 struct genl_family *f;
106 101
107 list_for_each_entry(f, genl_family_chain(id), family_list)102 idr_for_each_entry(&genl_fam_idr, family, id)
108 if (f->id == id)103 if (strcmp(family->name, name) == 0)
109 return f;104 return family;
110 105
111 return NULL;106 return NULL;
112}107}
113 108
114static struct genl_family *genl_family_find_byname(char *name)109static const struct genl_ops *genl_get_cmd(u8 cmd,
115{110 const struct genl_family *family)
116 struct genl_family *f;
117 int i;
118
119 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
120 list_for_each_entry(f, genl_family_chain(i), family_list)
121 if (strcmp(f->name, name) == 0)
122 return f;
123
124 return NULL;
125}
126
127static const struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
128{111{
129 int i;112 int i;
130 113
@@ -135,26 +118,6 @@ static const struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family) · 4 unmodified lines
135 return NULL;118 return NULL;
136}119}
137 120
138/* Of course we are going to have problems once we hit
139 * 2^16 alive types, but that can only happen by year 2K
140*/
141static u16 genl_generate_id(void)
142{
143 static u16 id_gen_idx = GENL_MIN_ID;
144 int i;
145
146 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
147 if (id_gen_idx != GENL_ID_VFS_DQUOT &&
148 id_gen_idx != GENL_ID_PMCRAID &&
149 !genl_family_find_byid(id_gen_idx))
150 return id_gen_idx;
151 if (++id_gen_idx > GENL_MAX_ID)
152 id_gen_idx = GENL_MIN_ID;
153 }
154
155 return 0;
156}
157
158static int genl_allocate_reserve_groups(int n_groups, int *first_id)121static int genl_allocate_reserve_groups(int n_groups, int *first_id)
159{122{
160 unsigned long *new_groups;123 unsigned long *new_groups;
@@ -295,7 +258,7 @@ static int genl_validate_assign_mc_groups(struct genl_family *family) · 134 unmodified lines
295 return err;258 return err;
296}259}
297 260
298static void genl_unregister_mc_groups(struct genl_family *family)261static void genl_unregister_mc_groups(const struct genl_family *family)
299{262{
300 struct net *net;263 struct net *net;
301 int i;264 int i;
@@ -358,6 +321,7 @@ static int genl_validate_ops(const struct genl_family *family) · 56 unmodified lines
358int genl_register_family(struct genl_family *family)321int genl_register_family(struct genl_family *family)
359{322{
360 int err, i;323 int err, i;
324 int start = GENL_START_ALLOC, end = GENL_MAX_ID;
361 325
362 err = genl_validate_ops(family);326 err = genl_validate_ops(family);
363 if (err)327 if (err)
@@ -370,34 +334,20 @@ int genl_register_family(struct genl_family *family) · 6 unmodified lines
370 goto errout_locked;334 goto errout_locked;
371 }335 }
372 336
337 /*
338 * Sadly, a few cases need to be special-cased
339 * due to them having previously abused the API
340 * and having used their family ID also as their
341 * multicast group ID, so we use reserved IDs
342 * for both to be sure we can do that mapping.
343 */
373 if (family == &genl_ctrl) {344 if (family == &genl_ctrl) {
374 family->id = GENL_ID_CTRL;345 /* and this needs to be special for initial family lookups */
375 } else {346 start = end = GENL_ID_CTRL;
376 u16 newid;347 } else if (strcmp(family->name, "pmcraid") == 0) {
377 348 start = end = GENL_ID_PMCRAID;
378 /* this should be left zero in the struct */349 } else if (strcmp(family->name, "VFS_DQUOT") == 0) {
379 WARN_ON(family->id);350 start = end = GENL_ID_VFS_DQUOT;
380
381 /*
382 * Sadly, a few cases need to be special-cased
383 * due to them having previously abused the API
384 * and having used their family ID also as their
385 * multicast group ID, so we use reserved IDs
386 * for both to be sure we can do that mapping.
387 */
388 if (strcmp(family->name, "pmcraid") == 0)
389 newid = GENL_ID_PMCRAID;
390 else if (strcmp(family->name, "VFS_DQUOT") == 0)
391 newid = GENL_ID_VFS_DQUOT;
392 else
393 newid = genl_generate_id();
394
395 if (!newid) {
396 err = -ENOMEM;
397 goto errout_locked;
398 }
399
400 family->id = newid;
401 }351 }
402 352
403 if (family->maxattr && !family->parallel_ops) {353 if (family->maxattr && !family->parallel_ops) {
@@ -410,11 +360,15 @@ int genl_register_family(struct genl_family *family) · 6 unmodified lines
410 } else360 } else
411 family->attrbuf = NULL;361 family->attrbuf = NULL;
412 362
363 family->id = idr_alloc(&genl_fam_idr, family,
364 start, end + 1, GFP_KERNEL);
365 if (!family->id)
366 goto errout_locked;
367
413 err = genl_validate_assign_mc_groups(family);368 err = genl_validate_assign_mc_groups(family);
414 if (err)369 if (err)
415 goto errout_locked;370 goto errout_remove;
416 371
417 list_add_tail(&family->family_list, genl_family_chain(family->id));
418 genl_unlock_all();372 genl_unlock_all();
419 373
420 /* send all events */374 /* send all events */
@@ -425,6 +379,8 @@ int genl_register_family(struct genl_family *family) · 4 unmodified lines
425 379
426 return 0;380 return 0;
427 381
382errout_remove:
383 idr_remove(&genl_fam_idr, family->id);
428errout_locked:384errout_locked:
429 genl_unlock_all();385 genl_unlock_all();
430 return err;386 return err;
@@ -439,32 +395,29 @@ EXPORT_SYMBOL(genl_register_family); · 8 unmodified lines
439 *395 *
440 * Returns 0 on success or a negative error code.396 * Returns 0 on success or a negative error code.
441 */397 */
442int genl_unregister_family(struct genl_family *family)398int genl_unregister_family(const struct genl_family *family)
443{399{
444 struct genl_family *rc;
445
446 genl_lock_all();400 genl_lock_all();
447 401
448 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {402 if (genl_family_find_byid(family->id)) {
449 if (family->id != rc->id || strcmp(rc->name, family->name))403 genl_unlock_all();
450 continue;404 return -ENOENT;
405 }
451 406
452 genl_unregister_mc_groups(family);407 genl_unregister_mc_groups(family);
453 408
454 list_del(&rc->family_list);409 idr_remove(&genl_fam_idr, family->id);
455 up_write(&cb_lock);
456 wait_event(genl_sk_destructing_waitq,
457 atomic_read(&genl_sk_destructing_cnt) == 0);
458 genl_unlock();
459 410
460 kfree(family->attrbuf);411 up_write(&cb_lock);
461 genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);412 wait_event(genl_sk_destructing_waitq,
462 return 0;413 atomic_read(&genl_sk_destructing_cnt) == 0);
463 }414 genl_unlock();
464 415
465 genl_unlock_all();416 kfree(family->attrbuf);
466 417
467 return -ENOENT;418 genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
419
420 return 0;
468}421}
469EXPORT_SYMBOL(genl_unregister_family);422EXPORT_SYMBOL(genl_unregister_family);
470 423
@@ -480,7 +433,7 @@ EXPORT_SYMBOL(genl_unregister_family); · 9 unmodified lines
480 * Returns pointer to user specific header433 * Returns pointer to user specific header
481 */434 */
482void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,435void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
483 struct genl_family *family, int flags, u8 cmd)436 const struct genl_family *family, int flags, u8 cmd)
484{437{
485 struct nlmsghdr *nlh;438 struct nlmsghdr *nlh;
486 struct genlmsghdr *hdr;439 struct genlmsghdr *hdr;
@@ -539,7 +492,7 @@ static int genl_lock_done(struct netlink_callback *cb) · 52 unmodified lines
539 return rc;492 return rc;
540}493}
541 494
542static int genl_family_rcv_msg(struct genl_family *family,495static int genl_family_rcv_msg(const struct genl_family *family,
543 struct sk_buff *skb,496 struct sk_buff *skb,
544 struct nlmsghdr *nlh)497 struct nlmsghdr *nlh)
545{498{
@@ -651,7 +604,7 @@ out: · 105 unmodified lines
651 604
652static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)605static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
653{606{
654 struct genl_family *family;607 const struct genl_family *family;
655 int err;608 int err;
656 609
657 family = genl_family_find_byid(nlh->nlmsg_type);610 family = genl_family_find_byid(nlh->nlmsg_type);
@@ -682,7 +635,7 @@ static void genl_rcv(struct sk_buff *skb) · 24 unmodified lines
682 635
683static struct genl_family genl_ctrl;636static struct genl_family genl_ctrl;
684 637
685static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,638static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq,
686 u32 flags, struct sk_buff *skb, u8 cmd)639 u32 flags, struct sk_buff *skb, u8 cmd)
687{640{
688 void *hdr;641 void *hdr;
@@ -769,7 +722,7 @@ nla_put_failure: · 80 unmodified lines
769 return -EMSGSIZE;722 return -EMSGSIZE;
770}723}
771 724
772static int ctrl_fill_mcgrp_info(struct genl_family *family,725static int ctrl_fill_mcgrp_info(const struct genl_family *family,
773 const struct genl_multicast_group *grp,726 const struct genl_multicast_group *grp,
774 int grp_id, u32 portid, u32 seq, u32 flags,727 int grp_id, u32 portid, u32 seq, u32 flags,
775 struct sk_buff *skb, u8 cmd)728 struct sk_buff *skb, u8 cmd)
@@ -812,37 +765,30 @@ nla_put_failure: · 36 unmodified lines
812 765
813static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)766static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
814{767{
815 768 int n = 0;
816 int i, n = 0;
817 struct genl_family *rt;769 struct genl_family *rt;
818 struct net *net = sock_net(skb->sk);770 struct net *net = sock_net(skb->sk);
819 int chains_to_skip = cb->args[0];771 int fams_to_skip = cb->args[0];
820 int fams_to_skip = cb->args[1];772 unsigned int id;
821
822 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
823 n = 0;
824 list_for_each_entry(rt, genl_family_chain(i), family_list) {
825 if (!rt->netnsok && !net_eq(net, &init_net))
826 continue;
827 if (++n < fams_to_skip)
828 continue;
829 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
830 cb->nlh->nlmsg_seq, NLM_F_MULTI,
831 skb, CTRL_CMD_NEWFAMILY) < 0)
832 goto errout;
833 }
834 773
835 fams_to_skip = 0;774 idr_for_each_entry(&genl_fam_idr, rt, id) {
836 }775 if (!rt->netnsok && !net_eq(net, &init_net))
776 continue;
777
778 if (n++ < fams_to_skip)
779 continue;
837 780
838errout:781 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
839 cb->args[0] = i;782 cb->nlh->nlmsg_seq, NLM_F_MULTI,
840 cb->args[1] = n;783 skb, CTRL_CMD_NEWFAMILY) < 0)
784 break;
785 }
841 786
787 cb->args[0] = n;
842 return skb->len;788 return skb->len;
843}789}
844 790
845static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,791static struct sk_buff *ctrl_build_family_msg(const struct genl_family *family,
846 u32 portid, int seq, u8 cmd)792 u32 portid, int seq, u8 cmd)
847{793{
848 struct sk_buff *skb;794 struct sk_buff *skb;
@@ -862,7 +808,7 @@ static struct sk_buff *ctrl_build_family_msg(struct genl_family *family, · 13 unmodified lines
862}808}
863 809
864static struct sk_buff *810static struct sk_buff *
865ctrl_build_mcgrp_msg(struct genl_family *family,811ctrl_build_mcgrp_msg(const struct genl_family *family,
866 const struct genl_multicast_group *grp,812 const struct genl_multicast_group *grp,
867 int grp_id, u32 portid, int seq, u8 cmd)813 int grp_id, u32 portid, int seq, u8 cmd)
868{814{
@@ -892,7 +838,7 @@ static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = { · 23 unmodified lines
892static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)838static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
893{839{
894 struct sk_buff *msg;840 struct sk_buff *msg;
895 struct genl_family *res = NULL;841 const struct genl_family *res = NULL;
896 int err = -EINVAL;842 int err = -EINVAL;
897 843
898 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {844 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
@@ -936,7 +882,7 @@ static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info) · 37 unmodified lines
936 return genlmsg_reply(msg, info);882 return genlmsg_reply(msg, info);
937}883}
938 884
939static int genl_ctrl_event(int event, struct genl_family *family,885static int genl_ctrl_event(int event, const struct genl_family *family,
940 const struct genl_multicast_group *grp,886 const struct genl_multicast_group *grp,
941 int grp_id)887 int grp_id)
942{888{
@@ -1005,25 +951,24 @@ static struct genl_family genl_ctrl = { · 62 unmodified lines
1005 951
1006static int genl_bind(struct net *net, int group)952static int genl_bind(struct net *net, int group)
1007{953{
1008 int i, err = -ENOENT;954 struct genl_family *f;
955 int err = -ENOENT;
956 unsigned int id;
1009 957
1010 down_read(&cb_lock);958 down_read(&cb_lock);
1011 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {959
1012 struct genl_family *f;960 idr_for_each_entry(&genl_fam_idr, f, id) {
1013 961 if (group >= f->mcgrp_offset &&
1014 list_for_each_entry(f, genl_family_chain(i), family_list) {962 group < f->mcgrp_offset + f->n_mcgrps) {
1015 if (group >= f->mcgrp_offset &&963 int fam_grp = group - f->mcgrp_offset;
1016 group < f->mcgrp_offset + f->n_mcgrps) {964
1017 int fam_grp = group - f->mcgrp_offset;965 if (!f->netnsok && net != &init_net)
1018 966 err = -ENOENT;
1019 if (!f->netnsok && net != &init_net)967 else if (f->mcast_bind)
1020 err = -ENOENT;968 err = f->mcast_bind(net, fam_grp);
1021 else if (f->mcast_bind)969 else
1022 err = f->mcast_bind(net, fam_grp);970 err = 0;
1023 else971 break;
1024 err = 0;
1025 break;
1026 }
1027 }972 }
1028 }973 }
1029 up_read(&cb_lock);974 up_read(&cb_lock);
@@ -1033,21 +978,19 @@ static int genl_bind(struct net *net, int group) · 3 unmodified lines
1033 978
1034static void genl_unbind(struct net *net, int group)979static void genl_unbind(struct net *net, int group)
1035{980{
1036 int i;981 struct genl_family *f;
982 unsigned int id;
1037 983
1038 down_read(&cb_lock);984 down_read(&cb_lock);
1039 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
1040 struct genl_family *f;
1041 985
1042 list_for_each_entry(f, genl_family_chain(i), family_list) {986 idr_for_each_entry(&genl_fam_idr, f, id) {
1043 if (group >= f->mcgrp_offset &&987 if (group >= f->mcgrp_offset &&
1044 group < f->mcgrp_offset + f->n_mcgrps) {988 group < f->mcgrp_offset + f->n_mcgrps) {
1045 int fam_grp = group - f->mcgrp_offset;989 int fam_grp = group - f->mcgrp_offset;
1046 990
1047 if (f->mcast_unbind)991 if (f->mcast_unbind)
1048 f->mcast_unbind(net, fam_grp);992 f->mcast_unbind(net, fam_grp);
1049 break;993 break;
1050 }
1051 }994 }
1052 }995 }
1053 up_read(&cb_lock);996 up_read(&cb_lock);
@@ -1087,10 +1030,7 @@ static struct pernet_operations genl_pernet_ops = { · 33 unmodified lines
1087 1030
1088static int __init genl_init(void)1031static int __init genl_init(void)
1089{1032{
1090 int i, err;1033 int err;
1091
1092 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
1093 INIT_LIST_HEAD(&family_ht[i]);
1094 1034
1095 err = genl_register_family(&genl_ctrl);1035 err = genl_register_family(&genl_ctrl);
1096 if (err < 0)1036 if (err < 0)
@@ -1118,7 +1058,7 @@ subsys_initcall(genl_init); · 21 unmodified lines
1118 * You cannot use this function with a family that has parallel_ops1058 * You cannot use this function with a family that has parallel_ops
1119 * and you can only use it within (pre/post) doit/dumpit callbacks.1059 * and you can only use it within (pre/post) doit/dumpit callbacks.
1120 */1060 */
1121struct nlattr **genl_family_attrbuf(struct genl_family *family)1061struct nlattr **genl_family_attrbuf(const struct genl_family *family)
1122{1062{
1123 if (!WARN_ON(family->parallel_ops))1063 if (!WARN_ON(family->parallel_ops))
1124 lockdep_assert_held(&genl_mutex);1064 lockdep_assert_held(&genl_mutex);
@@ -1156,8 +1096,9 @@ static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group, · 31 unmodified lines
1156 return err;1096 return err;
1157}1097}
1158 1098
1159int genlmsg_multicast_allns(struct genl_family *family, struct sk_buff *skb,1099int genlmsg_multicast_allns(const struct genl_family *family,
1160 u32 portid, unsigned int group, gfp_t flags)1100 struct sk_buff *skb, u32 portid,
1101 unsigned int group, gfp_t flags)
1161{1102{
1162 if (WARN_ON_ONCE(group >= family->n_mcgrps))1103 if (WARN_ON_ONCE(group >= family->n_mcgrps))
1163 return -EINVAL;1104 return -EINVAL;
@@ -1166,7 +1107,7 @@ int genlmsg_multicast_allns(struct genl_family *family, struct sk_buff *skb, · 2 unmodified lines
1166}1107}
1167EXPORT_SYMBOL(genlmsg_multicast_allns);1108EXPORT_SYMBOL(genlmsg_multicast_allns);
1168 1109
1169void genl_notify(struct genl_family *family, struct sk_buff *skb,1110void genl_notify(const struct genl_family *family, struct sk_buff *skb,
1170 struct genl_info *info, u32 group, gfp_t flags)1111 struct genl_info *info, u32 group, gfp_t flags)
1171{1112{
1172 struct net *net = genl_info_net(info);1113 struct net *net = genl_info_net(info);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t