forked from mirrors/linux
		
	genetlink: use iterator in the op to policy map dumping
We can't put the full iterator in the struct ctrl_dump_policy_ctx because dump context is statically sized by netlink core. Allocate it dynamically. Rename policy to dump_map to make the logic a little easier to follow. Signed-off-by: Jakub Kicinski <kuba@kernel.org> Reviewed-by: Jacob Keller <jacob.e.keller@intel.com> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
		
							parent
							
								
									6557461cd2
								
							
						
					
					
						commit
						b502b3185c
					
				
					 1 changed files with 26 additions and 23 deletions
				
			
		| 
						 | 
					@ -1252,10 +1252,10 @@ static int genl_ctrl_event(int event, const struct genl_family *family,
 | 
				
			||||||
struct ctrl_dump_policy_ctx {
 | 
					struct ctrl_dump_policy_ctx {
 | 
				
			||||||
	struct netlink_policy_dump_state *state;
 | 
						struct netlink_policy_dump_state *state;
 | 
				
			||||||
	const struct genl_family *rt;
 | 
						const struct genl_family *rt;
 | 
				
			||||||
	unsigned int opidx;
 | 
						struct genl_op_iter *op_iter;
 | 
				
			||||||
	u32 op;
 | 
						u32 op;
 | 
				
			||||||
	u16 fam_id;
 | 
						u16 fam_id;
 | 
				
			||||||
	u8 policies:1,
 | 
						u8 dump_map:1,
 | 
				
			||||||
	   single_op:1;
 | 
						   single_op:1;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1325,9 +1325,16 @@ static int ctrl_dumppolicy_start(struct netlink_callback *cb)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (!ctx->state)
 | 
							if (!ctx->state)
 | 
				
			||||||
			return -ENODATA;
 | 
								return -ENODATA;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ctx->dump_map = 1;
 | 
				
			||||||
		return 0;
 | 
							return 0;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ctx->op_iter = kmalloc(sizeof(*ctx->op_iter), GFP_KERNEL);
 | 
				
			||||||
 | 
						if (!ctx->op_iter)
 | 
				
			||||||
 | 
							return -ENOMEM;
 | 
				
			||||||
 | 
						ctx->dump_map = genl_op_iter_init(rt, ctx->op_iter);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for (genl_op_iter_init(rt, &i); genl_op_iter_next(&i); ) {
 | 
						for (genl_op_iter_init(rt, &i); genl_op_iter_next(&i); ) {
 | 
				
			||||||
		if (i.doit.policy) {
 | 
							if (i.doit.policy) {
 | 
				
			||||||
			err = netlink_policy_dump_add_policy(&ctx->state,
 | 
								err = netlink_policy_dump_add_policy(&ctx->state,
 | 
				
			||||||
| 
						 | 
					@ -1345,12 +1352,16 @@ static int ctrl_dumppolicy_start(struct netlink_callback *cb)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!ctx->state)
 | 
						if (!ctx->state) {
 | 
				
			||||||
		return -ENODATA;
 | 
							err = -ENODATA;
 | 
				
			||||||
 | 
							goto err_free_op_iter;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
err_free_state:
 | 
					err_free_state:
 | 
				
			||||||
	netlink_policy_dump_free(ctx->state);
 | 
						netlink_policy_dump_free(ctx->state);
 | 
				
			||||||
 | 
					err_free_op_iter:
 | 
				
			||||||
 | 
						kfree(ctx->op_iter);
 | 
				
			||||||
	return err;
 | 
						return err;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1430,11 +1441,10 @@ static int ctrl_dumppolicy(struct sk_buff *skb, struct netlink_callback *cb)
 | 
				
			||||||
	struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
 | 
						struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
 | 
				
			||||||
	void *hdr;
 | 
						void *hdr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!ctx->policies) {
 | 
						if (ctx->dump_map) {
 | 
				
			||||||
		struct genl_split_ops doit, dumpit;
 | 
					 | 
				
			||||||
		struct genl_ops op;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (ctx->single_op) {
 | 
							if (ctx->single_op) {
 | 
				
			||||||
 | 
								struct genl_split_ops doit, dumpit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if (genl_get_cmd(ctx->op, GENL_CMD_CAP_DO,
 | 
								if (genl_get_cmd(ctx->op, GENL_CMD_CAP_DO,
 | 
				
			||||||
					 ctx->rt, &doit) &&
 | 
										 ctx->rt, &doit) &&
 | 
				
			||||||
			    genl_get_cmd(ctx->op, GENL_CMD_CAP_DUMP,
 | 
								    genl_get_cmd(ctx->op, GENL_CMD_CAP_DUMP,
 | 
				
			||||||
| 
						 | 
					@ -1446,26 +1456,18 @@ static int ctrl_dumppolicy(struct sk_buff *skb, struct netlink_callback *cb)
 | 
				
			||||||
			if (ctrl_dumppolicy_put_op(skb, cb, &doit, &dumpit))
 | 
								if (ctrl_dumppolicy_put_op(skb, cb, &doit, &dumpit))
 | 
				
			||||||
				return skb->len;
 | 
									return skb->len;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			/* don't enter the loop below */
 | 
								/* done with the per-op policy index list */
 | 
				
			||||||
			ctx->opidx = genl_get_cmd_cnt(ctx->rt);
 | 
								ctx->dump_map = 0;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		while (ctx->opidx < genl_get_cmd_cnt(ctx->rt)) {
 | 
							while (ctx->dump_map) {
 | 
				
			||||||
			genl_get_cmd_by_index(ctx->opidx, ctx->rt, &op);
 | 
								if (ctrl_dumppolicy_put_op(skb, cb,
 | 
				
			||||||
 | 
											   &ctx->op_iter->doit,
 | 
				
			||||||
			genl_cmd_full_to_split(&doit, ctx->rt,
 | 
											   &ctx->op_iter->dumpit))
 | 
				
			||||||
					       &op, GENL_CMD_CAP_DO);
 | 
					 | 
				
			||||||
			genl_cmd_full_to_split(&dumpit, ctx->rt,
 | 
					 | 
				
			||||||
					       &op, GENL_CMD_CAP_DUMP);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			if (ctrl_dumppolicy_put_op(skb, cb, &doit, &dumpit))
 | 
					 | 
				
			||||||
				return skb->len;
 | 
									return skb->len;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			ctx->opidx++;
 | 
								ctx->dump_map = genl_op_iter_next(ctx->op_iter);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					 | 
				
			||||||
		/* completed with the per-op policy index list */
 | 
					 | 
				
			||||||
		ctx->policies = true;
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	while (netlink_policy_dump_loop(ctx->state)) {
 | 
						while (netlink_policy_dump_loop(ctx->state)) {
 | 
				
			||||||
| 
						 | 
					@ -1498,6 +1500,7 @@ static int ctrl_dumppolicy_done(struct netlink_callback *cb)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
 | 
						struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						kfree(ctx->op_iter);
 | 
				
			||||||
	netlink_policy_dump_free(ctx->state);
 | 
						netlink_policy_dump_free(ctx->state);
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue