Skip to content

Commit d59ed9d

Browse files
ummakynesgregkh
authored andcommitted
netfilter: add helper function to set up the nfnetlink header and use it
[ 19c28b1 ] This patch adds a helper function to set up the netlink and nfnetlink headers. Update existing codebase to use it. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent fa498de commit d59ed9d

File tree

11 files changed

+102
-208
lines changed

11 files changed

+102
-208
lines changed

include/linux/netfilter/nfnetlink.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,33 @@ static inline u16 nfnl_msg_type(u8 subsys, u8 msg_type)
5656
return subsys << 8 | msg_type;
5757
}
5858

59+
static inline void nfnl_fill_hdr(struct nlmsghdr *nlh, u8 family, u8 version,
60+
__be16 res_id)
61+
{
62+
struct nfgenmsg *nfmsg;
63+
64+
nfmsg = nlmsg_data(nlh);
65+
nfmsg->nfgen_family = family;
66+
nfmsg->version = version;
67+
nfmsg->res_id = res_id;
68+
}
69+
70+
static inline struct nlmsghdr *nfnl_msg_put(struct sk_buff *skb, u32 portid,
71+
u32 seq, int type, int flags,
72+
u8 family, u8 version,
73+
__be16 res_id)
74+
{
75+
struct nlmsghdr *nlh;
76+
77+
nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), flags);
78+
if (!nlh)
79+
return NULL;
80+
81+
nfnl_fill_hdr(nlh, family, version, res_id);
82+
83+
return nlh;
84+
}
85+
5986
void nfnl_lock(__u8 subsys_id);
6087
void nfnl_unlock(__u8 subsys_id);
6188
#ifdef CONFIG_PROVE_LOCKING

net/netfilter/ipset/ip_set_core.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -811,20 +811,9 @@ static struct nlmsghdr *
811811
start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags,
812812
enum ipset_cmd cmd)
813813
{
814-
struct nlmsghdr *nlh;
815-
struct nfgenmsg *nfmsg;
816-
817-
nlh = nlmsg_put(skb, portid, seq, nfnl_msg_type(NFNL_SUBSYS_IPSET, cmd),
818-
sizeof(*nfmsg), flags);
819-
if (!nlh)
820-
return NULL;
821-
822-
nfmsg = nlmsg_data(nlh);
823-
nfmsg->nfgen_family = NFPROTO_IPV4;
824-
nfmsg->version = NFNETLINK_V0;
825-
nfmsg->res_id = 0;
826-
827-
return nlh;
814+
return nfnl_msg_put(skb, portid, seq,
815+
nfnl_msg_type(NFNL_SUBSYS_IPSET, cmd), flags,
816+
NFPROTO_IPV4, NFNETLINK_V0, 0);
828817
}
829818

830819
/* Create a set */

net/netfilter/nf_conntrack_netlink.c

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -515,20 +515,15 @@ ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
515515
{
516516
const struct nf_conntrack_zone *zone;
517517
struct nlmsghdr *nlh;
518-
struct nfgenmsg *nfmsg;
519518
struct nlattr *nest_parms;
520519
unsigned int flags = portid ? NLM_F_MULTI : 0, event;
521520

522521
event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
523-
nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
524-
if (nlh == NULL)
522+
nlh = nfnl_msg_put(skb, portid, seq, event, flags, nf_ct_l3num(ct),
523+
NFNETLINK_V0, 0);
524+
if (!nlh)
525525
goto nlmsg_failure;
526526

527-
nfmsg = nlmsg_data(nlh);
528-
nfmsg->nfgen_family = nf_ct_l3num(ct);
529-
nfmsg->version = NFNETLINK_V0;
530-
nfmsg->res_id = 0;
531-
532527
zone = nf_ct_zone(ct);
533528

534529
nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG);
@@ -685,7 +680,6 @@ ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
685680
const struct nf_conntrack_zone *zone;
686681
struct net *net;
687682
struct nlmsghdr *nlh;
688-
struct nfgenmsg *nfmsg;
689683
struct nlattr *nest_parms;
690684
struct nf_conn *ct = item->ct;
691685
struct sk_buff *skb;
@@ -715,15 +709,11 @@ ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
715709
goto errout;
716710

717711
type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
718-
nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
719-
if (nlh == NULL)
712+
nlh = nfnl_msg_put(skb, item->portid, 0, type, flags, nf_ct_l3num(ct),
713+
NFNETLINK_V0, 0);
714+
if (!nlh)
720715
goto nlmsg_failure;
721716

722-
nfmsg = nlmsg_data(nlh);
723-
nfmsg->nfgen_family = nf_ct_l3num(ct);
724-
nfmsg->version = NFNETLINK_V0;
725-
nfmsg->res_id = 0;
726-
727717
zone = nf_ct_zone(ct);
728718

729719
nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG);
@@ -2200,20 +2190,15 @@ ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
22002190
__u16 cpu, const struct ip_conntrack_stat *st)
22012191
{
22022192
struct nlmsghdr *nlh;
2203-
struct nfgenmsg *nfmsg;
22042193
unsigned int flags = portid ? NLM_F_MULTI : 0, event;
22052194

22062195
event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
22072196
IPCTNL_MSG_CT_GET_STATS_CPU);
2208-
nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2209-
if (nlh == NULL)
2197+
nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
2198+
NFNETLINK_V0, htons(cpu));
2199+
if (!nlh)
22102200
goto nlmsg_failure;
22112201

2212-
nfmsg = nlmsg_data(nlh);
2213-
nfmsg->nfgen_family = AF_UNSPEC;
2214-
nfmsg->version = NFNETLINK_V0;
2215-
nfmsg->res_id = htons(cpu);
2216-
22172202
if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
22182203
nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
22192204
nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
@@ -2284,20 +2269,15 @@ ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
22842269
struct net *net)
22852270
{
22862271
struct nlmsghdr *nlh;
2287-
struct nfgenmsg *nfmsg;
22882272
unsigned int flags = portid ? NLM_F_MULTI : 0, event;
22892273
unsigned int nr_conntracks = atomic_read(&net->ct.count);
22902274

22912275
event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2292-
nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2293-
if (nlh == NULL)
2276+
nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
2277+
NFNETLINK_V0, 0);
2278+
if (!nlh)
22942279
goto nlmsg_failure;
22952280

2296-
nfmsg = nlmsg_data(nlh);
2297-
nfmsg->nfgen_family = AF_UNSPEC;
2298-
nfmsg->version = NFNETLINK_V0;
2299-
nfmsg->res_id = 0;
2300-
23012281
if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
23022282
goto nla_put_failure;
23032283

@@ -2803,19 +2783,14 @@ ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
28032783
int event, const struct nf_conntrack_expect *exp)
28042784
{
28052785
struct nlmsghdr *nlh;
2806-
struct nfgenmsg *nfmsg;
28072786
unsigned int flags = portid ? NLM_F_MULTI : 0;
28082787

28092788
event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2810-
nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2811-
if (nlh == NULL)
2789+
nlh = nfnl_msg_put(skb, portid, seq, event, flags,
2790+
exp->tuple.src.l3num, NFNETLINK_V0, 0);
2791+
if (!nlh)
28122792
goto nlmsg_failure;
28132793

2814-
nfmsg = nlmsg_data(nlh);
2815-
nfmsg->nfgen_family = exp->tuple.src.l3num;
2816-
nfmsg->version = NFNETLINK_V0;
2817-
nfmsg->res_id = 0;
2818-
28192794
if (ctnetlink_exp_dump_expect(skb, exp) < 0)
28202795
goto nla_put_failure;
28212796

@@ -2835,7 +2810,6 @@ ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
28352810
struct nf_conntrack_expect *exp = item->exp;
28362811
struct net *net = nf_ct_exp_net(exp);
28372812
struct nlmsghdr *nlh;
2838-
struct nfgenmsg *nfmsg;
28392813
struct sk_buff *skb;
28402814
unsigned int type, group;
28412815
int flags = 0;
@@ -2858,15 +2832,11 @@ ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
28582832
goto errout;
28592833

28602834
type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2861-
nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2862-
if (nlh == NULL)
2835+
nlh = nfnl_msg_put(skb, item->portid, 0, type, flags,
2836+
exp->tuple.src.l3num, NFNETLINK_V0, 0);
2837+
if (!nlh)
28632838
goto nlmsg_failure;
28642839

2865-
nfmsg = nlmsg_data(nlh);
2866-
nfmsg->nfgen_family = exp->tuple.src.l3num;
2867-
nfmsg->version = NFNETLINK_V0;
2868-
nfmsg->res_id = 0;
2869-
28702840
if (ctnetlink_exp_dump_expect(skb, exp) < 0)
28712841
goto nla_put_failure;
28722842

@@ -3436,20 +3406,15 @@ ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
34363406
const struct ip_conntrack_stat *st)
34373407
{
34383408
struct nlmsghdr *nlh;
3439-
struct nfgenmsg *nfmsg;
34403409
unsigned int flags = portid ? NLM_F_MULTI : 0, event;
34413410

34423411
event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
34433412
IPCTNL_MSG_EXP_GET_STATS_CPU);
3444-
nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3445-
if (nlh == NULL)
3413+
nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
3414+
NFNETLINK_V0, htons(cpu));
3415+
if (!nlh)
34463416
goto nlmsg_failure;
34473417

3448-
nfmsg = nlmsg_data(nlh);
3449-
nfmsg->nfgen_family = AF_UNSPEC;
3450-
nfmsg->version = NFNETLINK_V0;
3451-
nfmsg->res_id = htons(cpu);
3452-
34533418
if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
34543419
nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
34553420
nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))

0 commit comments

Comments
 (0)