Skip to content

Commit 737535c

Browse files
author
Jan Engelhardt
committed
netfilter: xtables: compact table hook functions (1/2)
This patch combines all the per-hook functions in a given table into a single function. Together with the 2nd patch, further simplifications are possible up to the point of output code reduction. Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
1 parent 9ab99d5 commit 737535c

9 files changed

Lines changed: 126 additions & 258 deletions

File tree

‎net/ipv4/netfilter/arptable_filter.c‎

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,37 @@ static const struct xt_table packet_filter = {
5353
};
5454

5555
/* The work comes in here from netfilter.c */
56-
static unsigned int arpt_in_hook(unsigned int hook,
57-
struct sk_buff *skb,
58-
const struct net_device *in,
59-
const struct net_device *out,
60-
int (*okfn)(struct sk_buff *))
56+
static unsigned int
57+
arptable_filter_hook(unsigned int hook, struct sk_buff *skb,
58+
const struct net_device *in, const struct net_device *out,
59+
int (*okfn)(struct sk_buff *))
6160
{
62-
return arpt_do_table(skb, hook, in, out,
63-
dev_net(in)->ipv4.arptable_filter);
64-
}
61+
if (hook == NF_ARP_OUT)
62+
return arpt_do_table(skb, hook, in, out,
63+
dev_net(out)->ipv4.arptable_filter);
6564

66-
static unsigned int arpt_out_hook(unsigned int hook,
67-
struct sk_buff *skb,
68-
const struct net_device *in,
69-
const struct net_device *out,
70-
int (*okfn)(struct sk_buff *))
71-
{
65+
/* INPUT/FORWARD: */
7266
return arpt_do_table(skb, hook, in, out,
73-
dev_net(out)->ipv4.arptable_filter);
67+
dev_net(in)->ipv4.arptable_filter);
7468
}
7569

7670
static struct nf_hook_ops arpt_ops[] __read_mostly = {
7771
{
78-
.hook = arpt_in_hook,
72+
.hook = arptable_filter_hook,
7973
.owner = THIS_MODULE,
8074
.pf = NFPROTO_ARP,
8175
.hooknum = NF_ARP_IN,
8276
.priority = NF_IP_PRI_FILTER,
8377
},
8478
{
85-
.hook = arpt_out_hook,
79+
.hook = arptable_filter_hook,
8680
.owner = THIS_MODULE,
8781
.pf = NFPROTO_ARP,
8882
.hooknum = NF_ARP_OUT,
8983
.priority = NF_IP_PRI_FILTER,
9084
},
9185
{
92-
.hook = arpt_in_hook,
86+
.hook = arptable_filter_hook,
9387
.owner = THIS_MODULE,
9488
.pf = NFPROTO_ARP,
9589
.hooknum = NF_ARP_FORWARD,

‎net/ipv4/netfilter/iptable_filter.c‎

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,61 +60,43 @@ static const struct xt_table packet_filter = {
6060
.af = NFPROTO_IPV4,
6161
};
6262

63-
/* The work comes in here from netfilter.c. */
6463
static unsigned int
65-
ipt_local_in_hook(unsigned int hook,
66-
struct sk_buff *skb,
67-
const struct net_device *in,
68-
const struct net_device *out,
69-
int (*okfn)(struct sk_buff *))
64+
iptable_filter_hook(unsigned int hook, struct sk_buff *skb,
65+
const struct net_device *in, const struct net_device *out,
66+
int (*okfn)(struct sk_buff *))
7067
{
71-
return ipt_do_table(skb, hook, in, out,
72-
dev_net(in)->ipv4.iptable_filter);
73-
}
68+
if (hook == NF_INET_LOCAL_OUT) {
69+
if (skb->len < sizeof(struct iphdr) ||
70+
ip_hdrlen(skb) < sizeof(struct iphdr))
71+
/* root is playing with raw sockets. */
72+
return NF_ACCEPT;
73+
74+
return ipt_do_table(skb, hook, in, out,
75+
dev_net(out)->ipv4.iptable_filter);
76+
}
7477

75-
static unsigned int
76-
ipt_hook(unsigned int hook,
77-
struct sk_buff *skb,
78-
const struct net_device *in,
79-
const struct net_device *out,
80-
int (*okfn)(struct sk_buff *))
81-
{
78+
/* LOCAL_IN/FORWARD: */
8279
return ipt_do_table(skb, hook, in, out,
8380
dev_net(in)->ipv4.iptable_filter);
8481
}
8582

86-
static unsigned int
87-
ipt_local_out_hook(unsigned int hook,
88-
struct sk_buff *skb,
89-
const struct net_device *in,
90-
const struct net_device *out,
91-
int (*okfn)(struct sk_buff *))
92-
{
93-
/* root is playing with raw sockets. */
94-
if (skb->len < sizeof(struct iphdr) ||
95-
ip_hdrlen(skb) < sizeof(struct iphdr))
96-
return NF_ACCEPT;
97-
return ipt_do_table(skb, hook, in, out,
98-
dev_net(out)->ipv4.iptable_filter);
99-
}
100-
10183
static struct nf_hook_ops ipt_ops[] __read_mostly = {
10284
{
103-
.hook = ipt_local_in_hook,
85+
.hook = iptable_filter_hook,
10486
.owner = THIS_MODULE,
10587
.pf = NFPROTO_IPV4,
10688
.hooknum = NF_INET_LOCAL_IN,
10789
.priority = NF_IP_PRI_FILTER,
10890
},
10991
{
110-
.hook = ipt_hook,
92+
.hook = iptable_filter_hook,
11193
.owner = THIS_MODULE,
11294
.pf = NFPROTO_IPV4,
11395
.hooknum = NF_INET_FORWARD,
11496
.priority = NF_IP_PRI_FILTER,
11597
},
11698
{
117-
.hook = ipt_local_out_hook,
99+
.hook = iptable_filter_hook,
118100
.owner = THIS_MODULE,
119101
.pf = NFPROTO_IPV4,
120102
.hooknum = NF_INET_LOCAL_OUT,

‎net/ipv4/netfilter/iptable_mangle.c‎

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -71,51 +71,6 @@ static const struct xt_table packet_mangler = {
7171
.af = NFPROTO_IPV4,
7272
};
7373

74-
/* The work comes in here from netfilter.c. */
75-
static unsigned int
76-
ipt_pre_routing_hook(unsigned int hook,
77-
struct sk_buff *skb,
78-
const struct net_device *in,
79-
const struct net_device *out,
80-
int (*okfn)(struct sk_buff *))
81-
{
82-
return ipt_do_table(skb, hook, in, out,
83-
dev_net(in)->ipv4.iptable_mangle);
84-
}
85-
86-
static unsigned int
87-
ipt_post_routing_hook(unsigned int hook,
88-
struct sk_buff *skb,
89-
const struct net_device *in,
90-
const struct net_device *out,
91-
int (*okfn)(struct sk_buff *))
92-
{
93-
return ipt_do_table(skb, hook, in, out,
94-
dev_net(out)->ipv4.iptable_mangle);
95-
}
96-
97-
static unsigned int
98-
ipt_local_in_hook(unsigned int hook,
99-
struct sk_buff *skb,
100-
const struct net_device *in,
101-
const struct net_device *out,
102-
int (*okfn)(struct sk_buff *))
103-
{
104-
return ipt_do_table(skb, hook, in, out,
105-
dev_net(in)->ipv4.iptable_mangle);
106-
}
107-
108-
static unsigned int
109-
ipt_forward_hook(unsigned int hook,
110-
struct sk_buff *skb,
111-
const struct net_device *in,
112-
const struct net_device *out,
113-
int (*okfn)(struct sk_buff *))
114-
{
115-
return ipt_do_table(skb, hook, in, out,
116-
dev_net(in)->ipv4.iptable_mangle);
117-
}
118-
11974
static unsigned int
12075
ipt_local_hook(unsigned int hook,
12176
struct sk_buff *skb,
@@ -158,37 +113,53 @@ ipt_local_hook(unsigned int hook,
158113
return ret;
159114
}
160115

116+
/* The work comes in here from netfilter.c. */
117+
static unsigned int
118+
iptable_mangle_hook(unsigned int hook,
119+
struct sk_buff *skb,
120+
const struct net_device *in,
121+
const struct net_device *out,
122+
int (*okfn)(struct sk_buff *))
123+
{
124+
if (hook == NF_INET_LOCAL_OUT)
125+
return ipt_local_hook(hook, skb, in, out, okfn);
126+
127+
/* PREROUTING/INPUT/FORWARD: */
128+
return ipt_do_table(skb, hook, in, out,
129+
dev_net(in)->ipv4.iptable_mangle);
130+
}
131+
161132
static struct nf_hook_ops ipt_ops[] __read_mostly = {
162133
{
163-
.hook = ipt_pre_routing_hook,
134+
.hook = iptable_mangle_hook,
164135
.owner = THIS_MODULE,
165136
.pf = NFPROTO_IPV4,
166137
.hooknum = NF_INET_PRE_ROUTING,
167138
.priority = NF_IP_PRI_MANGLE,
168139
},
169140
{
170-
.hook = ipt_local_in_hook,
141+
.hook = iptable_mangle_hook,
171142
.owner = THIS_MODULE,
172143
.pf = NFPROTO_IPV4,
173144
.hooknum = NF_INET_LOCAL_IN,
174145
.priority = NF_IP_PRI_MANGLE,
175146
},
176147
{
177-
.hook = ipt_forward_hook,
148+
.hook = iptable_mangle_hook,
178149
.owner = THIS_MODULE,
179150
.pf = NFPROTO_IPV4,
180151
.hooknum = NF_INET_FORWARD,
181152
.priority = NF_IP_PRI_MANGLE,
182153
},
183154
{
184-
.hook = ipt_local_hook,
155+
.hook = iptable_mangle_hook,
185156
.owner = THIS_MODULE,
186157
.pf = NFPROTO_IPV4,
187158
.hooknum = NF_INET_LOCAL_OUT,
188159
.priority = NF_IP_PRI_MANGLE,
189160
},
190161
{
191-
.hook = ipt_post_routing_hook,
162+
.hook = iptable_mangle_hook,
192163
.owner = THIS_MODULE,
193164
.pf = NFPROTO_IPV4,
194165
.hooknum = NF_INET_POST_ROUTING,

‎net/ipv4/netfilter/iptable_raw.c‎

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,15 @@ static const struct xt_table packet_raw = {
4545

4646
/* The work comes in here from netfilter.c. */
4747
static unsigned int
48-
ipt_hook(unsigned int hook,
49-
struct sk_buff *skb,
50-
const struct net_device *in,
51-
const struct net_device *out,
52-
int (*okfn)(struct sk_buff *))
48+
iptable_raw_hook(unsigned int hook, struct sk_buff *skb,
49+
const struct net_device *in, const struct net_device *out,
50+
int (*okfn)(struct sk_buff *))
5351
{
54-
return ipt_do_table(skb, hook, in, out,
55-
dev_net(in)->ipv4.iptable_raw);
56-
}
52+
if (hook == NF_INET_PRE_ROUTING)
53+
return ipt_do_table(skb, hook, in, out,
54+
dev_net(in)->ipv4.iptable_raw);
5755

58-
static unsigned int
59-
ipt_local_hook(unsigned int hook,
60-
struct sk_buff *skb,
61-
const struct net_device *in,
62-
const struct net_device *out,
63-
int (*okfn)(struct sk_buff *))
64-
{
56+
/* OUTPUT: */
6557
/* root is playing with raw sockets. */
6658
if (skb->len < sizeof(struct iphdr) ||
6759
ip_hdrlen(skb) < sizeof(struct iphdr))
@@ -73,14 +65,14 @@ ipt_local_hook(unsigned int hook,
7365
/* 'raw' is the very first table. */
7466
static struct nf_hook_ops ipt_ops[] __read_mostly = {
7567
{
76-
.hook = ipt_hook,
68+
.hook = iptable_raw_hook,
7769
.pf = NFPROTO_IPV4,
7870
.hooknum = NF_INET_PRE_ROUTING,
7971
.priority = NF_IP_PRI_RAW,
8072
.owner = THIS_MODULE,
8173
},
8274
{
83-
.hook = ipt_local_hook,
75+
.hook = iptable_raw_hook,
8476
.pf = NFPROTO_IPV4,
8577
.hooknum = NF_INET_LOCAL_OUT,
8678
.priority = NF_IP_PRI_RAW,

‎net/ipv4/netfilter/iptable_security.c‎

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,59 +65,43 @@ static const struct xt_table security_table = {
6565
};
6666

6767
static unsigned int
68-
ipt_local_in_hook(unsigned int hook,
69-
struct sk_buff *skb,
70-
const struct net_device *in,
71-
const struct net_device *out,
72-
int (*okfn)(struct sk_buff *))
68+
iptable_security_hook(unsigned int hook, struct sk_buff *skb,
69+
const struct net_device *in,
70+
const struct net_device *out,
71+
int (*okfn)(struct sk_buff *))
7372
{
74-
return ipt_do_table(skb, hook, in, out,
75-
dev_net(in)->ipv4.iptable_security);
76-
}
73+
if (hook == NF_INET_LOCAL_OUT) {
74+
if (skb->len < sizeof(struct iphdr) ||
75+
ip_hdrlen(skb) < sizeof(struct iphdr))
76+
/* Somebody is playing with raw sockets. */
77+
return NF_ACCEPT;
7778

78-
static unsigned int
79-
ipt_forward_hook(unsigned int hook,
80-
struct sk_buff *skb,
81-
const struct net_device *in,
82-
const struct net_device *out,
83-
int (*okfn)(struct sk_buff *))
84-
{
85-
return ipt_do_table(skb, hook, in, out,
86-
dev_net(in)->ipv4.iptable_security);
87-
}
79+
return ipt_do_table(skb, hook, in, out,
80+
dev_net(out)->ipv4.iptable_security);
81+
}
8882

89-
static unsigned int
90-
ipt_local_out_hook(unsigned int hook,
91-
struct sk_buff *skb,
92-
const struct net_device *in,
93-
const struct net_device *out,
94-
int (*okfn)(struct sk_buff *))
95-
{
96-
/* Somebody is playing with raw sockets. */
97-
if (skb->len < sizeof(struct iphdr) ||
98-
ip_hdrlen(skb) < sizeof(struct iphdr))
99-
return NF_ACCEPT;
83+
/* INPUT/FORWARD: */
10084
return ipt_do_table(skb, hook, in, out,
101-
dev_net(out)->ipv4.iptable_security);
85+
dev_net(in)->ipv4.iptable_security);
10286
}
10387

10488
static struct nf_hook_ops ipt_ops[] __read_mostly = {
10589
{
106-
.hook = ipt_local_in_hook,
90+
.hook = iptable_security_hook,
10791
.owner = THIS_MODULE,
10892
.pf = NFPROTO_IPV4,
10993
.hooknum = NF_INET_LOCAL_IN,
11094
.priority = NF_IP_PRI_SECURITY,
11195
},
11296
{
113-
.hook = ipt_forward_hook,
97+
.hook = iptable_security_hook,
11498
.owner = THIS_MODULE,
11599
.pf = NFPROTO_IPV4,
116100
.hooknum = NF_INET_FORWARD,
117101
.priority = NF_IP_PRI_SECURITY,
118102
},
119103
{
120-
.hook = ipt_local_out_hook,
104+
.hook = iptable_security_hook,
121105
.owner = THIS_MODULE,
122106
.pf = NFPROTO_IPV4,
123107
.hooknum = NF_INET_LOCAL_OUT,

0 commit comments

Comments
 (0)