Commit 17c157c8 authored by Thomas Graf's avatar Thomas Graf Committed by David S. Miller

[GENL]: Add genlmsg_put_reply() to simplify building reply headers

By modyfing genlmsg_put() to take a genl_family and by adding
genlmsg_put_reply() the process of constructing the netlink
and generic netlink headers is simplified.
Signed-off-by: default avatarThomas Graf <tgraf@suug.ch>
Acked-by: default avatarPaul Moore <paul.moore@hp.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 81878d27
...@@ -79,33 +79,50 @@ extern struct sock *genl_sock; ...@@ -79,33 +79,50 @@ extern struct sock *genl_sock;
* @skb: socket buffer holding the message * @skb: socket buffer holding the message
* @pid: netlink pid the message is addressed to * @pid: netlink pid the message is addressed to
* @seq: sequence number (usually the one of the sender) * @seq: sequence number (usually the one of the sender)
* @type: netlink message type * @family: generic netlink family
* @hdrlen: length of the user specific header
* @flags netlink message flags * @flags netlink message flags
* @cmd: generic netlink command * @cmd: generic netlink command
* @version: version
* *
* Returns pointer to user specific header * Returns pointer to user specific header
*/ */
static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq, static inline void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
int type, int hdrlen, int flags, struct genl_family *family, int flags, u8 cmd)
u8 cmd, u8 version)
{ {
struct nlmsghdr *nlh; struct nlmsghdr *nlh;
struct genlmsghdr *hdr; struct genlmsghdr *hdr;
nlh = nlmsg_put(skb, pid, seq, type, GENL_HDRLEN + hdrlen, flags); nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
family->hdrsize, flags);
if (nlh == NULL) if (nlh == NULL)
return NULL; return NULL;
hdr = nlmsg_data(nlh); hdr = nlmsg_data(nlh);
hdr->cmd = cmd; hdr->cmd = cmd;
hdr->version = version; hdr->version = family->version;
hdr->reserved = 0; hdr->reserved = 0;
return (char *) hdr + GENL_HDRLEN; return (char *) hdr + GENL_HDRLEN;
} }
/**
* genlmsg_put_reply - Add generic netlink header to a reply message
* @skb: socket buffer holding the message
* @info: receiver info
* @family: generic netlink family
* @flags: netlink message flags
* @cmd: generic netlink command
*
* Returns pointer to user specific header
*/
static inline void *genlmsg_put_reply(struct sk_buff *skb,
struct genl_info *info,
struct genl_family *family,
int flags, u8 cmd)
{
return genlmsg_put(skb, info->snd_pid, info->snd_seq, family,
flags, cmd);
}
/** /**
* genlmsg_end - Finalize a generic netlink message * genlmsg_end - Finalize a generic netlink message
* @skb: socket buffer the message is stored in * @skb: socket buffer the message is stored in
......
...@@ -85,13 +85,9 @@ static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp, ...@@ -85,13 +85,9 @@ static int prepare_reply(struct genl_info *info, u8 cmd, struct sk_buff **skbp,
int seq = get_cpu_var(taskstats_seqnum)++; int seq = get_cpu_var(taskstats_seqnum)++;
put_cpu_var(taskstats_seqnum); put_cpu_var(taskstats_seqnum);
reply = genlmsg_put(skb, 0, seq, reply = genlmsg_put(skb, 0, seq, &family, 0, cmd);
family.id, 0, 0,
cmd, family.version);
} else } else
reply = genlmsg_put(skb, info->snd_pid, info->snd_seq, reply = genlmsg_put_reply(skb, info, &family, 0, cmd);
family.id, 0, 0,
cmd, family.version);
if (reply == NULL) { if (reply == NULL) {
nlmsg_free(skb); nlmsg_free(skb);
return -EINVAL; return -EINVAL;
......
...@@ -457,12 +457,8 @@ static int netlbl_cipsov4_list(struct sk_buff *skb, struct genl_info *info) ...@@ -457,12 +457,8 @@ static int netlbl_cipsov4_list(struct sk_buff *skb, struct genl_info *info)
ret_val = -ENOMEM; ret_val = -ENOMEM;
goto list_failure; goto list_failure;
} }
data = netlbl_netlink_hdr_put(ans_skb, data = genlmsg_put_reply(ans_skb, info, &netlbl_cipsov4_gnl_family,
info->snd_pid, 0, NLBL_CIPSOV4_C_LIST);
info->snd_seq,
netlbl_cipsov4_gnl_family.id,
0,
NLBL_CIPSOV4_C_LIST);
if (data == NULL) { if (data == NULL) {
ret_val = -ENOMEM; ret_val = -ENOMEM;
goto list_failure; goto list_failure;
...@@ -607,12 +603,9 @@ static int netlbl_cipsov4_listall_cb(struct cipso_v4_doi *doi_def, void *arg) ...@@ -607,12 +603,9 @@ static int netlbl_cipsov4_listall_cb(struct cipso_v4_doi *doi_def, void *arg)
struct netlbl_cipsov4_doiwalk_arg *cb_arg = arg; struct netlbl_cipsov4_doiwalk_arg *cb_arg = arg;
void *data; void *data;
data = netlbl_netlink_hdr_put(cb_arg->skb, data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
NETLINK_CB(cb_arg->nl_cb->skb).pid, cb_arg->seq, &netlbl_cipsov4_gnl_family,
cb_arg->seq, NLM_F_MULTI, NLBL_CIPSOV4_C_LISTALL);
netlbl_cipsov4_gnl_family.id,
NLM_F_MULTI,
NLBL_CIPSOV4_C_LISTALL);
if (data == NULL) if (data == NULL)
goto listall_cb_failure; goto listall_cb_failure;
......
...@@ -188,12 +188,9 @@ static int netlbl_mgmt_listall_cb(struct netlbl_dom_map *entry, void *arg) ...@@ -188,12 +188,9 @@ static int netlbl_mgmt_listall_cb(struct netlbl_dom_map *entry, void *arg)
struct netlbl_domhsh_walk_arg *cb_arg = arg; struct netlbl_domhsh_walk_arg *cb_arg = arg;
void *data; void *data;
data = netlbl_netlink_hdr_put(cb_arg->skb, data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
NETLINK_CB(cb_arg->nl_cb->skb).pid, cb_arg->seq, &netlbl_mgmt_gnl_family,
cb_arg->seq, NLM_F_MULTI, NLBL_MGMT_C_LISTALL);
netlbl_mgmt_gnl_family.id,
NLM_F_MULTI,
NLBL_MGMT_C_LISTALL);
if (data == NULL) if (data == NULL)
goto listall_cb_failure; goto listall_cb_failure;
...@@ -359,12 +356,8 @@ static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info) ...@@ -359,12 +356,8 @@ static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info)
ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (ans_skb == NULL) if (ans_skb == NULL)
return -ENOMEM; return -ENOMEM;
data = netlbl_netlink_hdr_put(ans_skb, data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
info->snd_pid, 0, NLBL_MGMT_C_LISTDEF);
info->snd_seq,
netlbl_mgmt_gnl_family.id,
0,
NLBL_MGMT_C_LISTDEF);
if (data == NULL) if (data == NULL)
goto listdef_failure; goto listdef_failure;
...@@ -422,11 +415,8 @@ static int netlbl_mgmt_protocols_cb(struct sk_buff *skb, ...@@ -422,11 +415,8 @@ static int netlbl_mgmt_protocols_cb(struct sk_buff *skb,
int ret_val = -ENOMEM; int ret_val = -ENOMEM;
void *data; void *data;
data = netlbl_netlink_hdr_put(skb, data = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
NETLINK_CB(cb->skb).pid, &netlbl_mgmt_gnl_family, NLM_F_MULTI,
cb->nlh->nlmsg_seq,
netlbl_mgmt_gnl_family.id,
NLM_F_MULTI,
NLBL_MGMT_C_PROTOCOLS); NLBL_MGMT_C_PROTOCOLS);
if (data == NULL) if (data == NULL)
goto protocols_cb_failure; goto protocols_cb_failure;
...@@ -495,12 +485,8 @@ static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info) ...@@ -495,12 +485,8 @@ static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info)
ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (ans_skb == NULL) if (ans_skb == NULL)
return -ENOMEM; return -ENOMEM;
data = netlbl_netlink_hdr_put(ans_skb, data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
info->snd_pid, 0, NLBL_MGMT_C_VERSION);
info->snd_seq,
netlbl_mgmt_gnl_family.id,
0,
NLBL_MGMT_C_VERSION);
if (data == NULL) if (data == NULL)
goto version_failure; goto version_failure;
......
...@@ -141,12 +141,8 @@ static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info) ...@@ -141,12 +141,8 @@ static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (ans_skb == NULL) if (ans_skb == NULL)
goto list_failure; goto list_failure;
data = netlbl_netlink_hdr_put(ans_skb, data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
info->snd_pid, 0, NLBL_UNLABEL_C_LIST);
info->snd_seq,
netlbl_unlabel_gnl_family.id,
0,
NLBL_UNLABEL_C_LIST);
if (data == NULL) { if (data == NULL) {
ret_val = -ENOMEM; ret_val = -ENOMEM;
goto list_failure; goto list_failure;
......
...@@ -41,37 +41,6 @@ ...@@ -41,37 +41,6 @@
/* NetLabel NETLINK helper functions */ /* NetLabel NETLINK helper functions */
/**
* netlbl_netlink_hdr_put - Write the NETLINK buffers into a sk_buff
* @skb: the packet
* @pid: the PID of the receipient
* @seq: the sequence number
* @type: the generic NETLINK message family type
* @cmd: command
*
* Description:
* Write both a NETLINK nlmsghdr structure and a Generic NETLINK genlmsghdr
* struct to the packet. Returns a pointer to the start of the payload buffer
* on success or NULL on failure.
*
*/
static inline void *netlbl_netlink_hdr_put(struct sk_buff *skb,
u32 pid,
u32 seq,
int type,
int flags,
u8 cmd)
{
return genlmsg_put(skb,
pid,
seq,
type,
0,
flags,
cmd,
NETLBL_PROTO_VERSION);
}
/** /**
* netlbl_netlink_auditinfo - Fetch the audit information from a NETLINK msg * netlbl_netlink_auditinfo - Fetch the audit information from a NETLINK msg
* @skb: the packet * @skb: the packet
......
...@@ -384,6 +384,13 @@ static void genl_rcv(struct sock *sk, int len) ...@@ -384,6 +384,13 @@ static void genl_rcv(struct sock *sk, int len)
* Controller * Controller
**************************************************************************/ **************************************************************************/
static struct genl_family genl_ctrl = {
.id = GENL_ID_CTRL,
.name = "nlctrl",
.version = 0x1,
.maxattr = CTRL_ATTR_MAX,
};
static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq, static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
u32 flags, struct sk_buff *skb, u8 cmd) u32 flags, struct sk_buff *skb, u8 cmd)
{ {
...@@ -392,8 +399,7 @@ static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq, ...@@ -392,8 +399,7 @@ static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
void *hdr; void *hdr;
int idx = 1; int idx = 1;
hdr = genlmsg_put(skb, pid, seq, GENL_ID_CTRL, 0, flags, cmd, hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
family->version);
if (hdr == NULL) if (hdr == NULL)
return -1; return -1;
...@@ -562,13 +568,6 @@ static struct genl_ops genl_ctrl_ops = { ...@@ -562,13 +568,6 @@ static struct genl_ops genl_ctrl_ops = {
.policy = ctrl_policy, .policy = ctrl_policy,
}; };
static struct genl_family genl_ctrl = {
.id = GENL_ID_CTRL,
.name = "nlctrl",
.version = 0x1,
.maxattr = CTRL_ATTR_MAX,
};
static int __init genl_init(void) static int __init genl_init(void)
{ {
int i, err; int i, err;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment