Skip to content

Commit 01886bd

Browse files
committed
netfilter: remove hook_entries field from nf_hook_state
This field is only useful for nf_queue, so store it in the nf_queue_entry structure instead, away from the core path. Pass hook_head to nf_hook_slow(). Since we always have a valid entry on the first iteration in nf_iterate(), we can use 'do { ... } while (entry)' loop instead. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent c63cbc4 commit 01886bd

8 files changed

Lines changed: 20 additions & 25 deletions

File tree

‎include/linux/netfilter.h‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ struct nf_hook_state {
5454
struct net_device *out;
5555
struct sock *sk;
5656
struct net *net;
57-
struct nf_hook_entry __rcu *hook_entries;
5857
int (*okfn)(struct net *, struct sock *, struct sk_buff *);
5958
};
6059

@@ -81,7 +80,6 @@ struct nf_hook_entry {
8180
};
8281

8382
static inline void nf_hook_state_init(struct nf_hook_state *p,
84-
struct nf_hook_entry *hook_entry,
8583
unsigned int hook,
8684
u_int8_t pf,
8785
struct net_device *indev,
@@ -96,7 +94,6 @@ static inline void nf_hook_state_init(struct nf_hook_state *p,
9694
p->out = outdev;
9795
p->sk = sk;
9896
p->net = net;
99-
RCU_INIT_POINTER(p->hook_entries, hook_entry);
10097
p->okfn = okfn;
10198
}
10299

@@ -150,7 +147,8 @@ void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
150147
extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
151148
#endif
152149

153-
int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state);
150+
int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
151+
struct nf_hook_entry *entry);
154152

155153
/**
156154
* nf_hook - call a netfilter hook
@@ -179,10 +177,10 @@ static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
179177
if (hook_head) {
180178
struct nf_hook_state state;
181179

182-
nf_hook_state_init(&state, hook_head, hook, pf, indev, outdev,
180+
nf_hook_state_init(&state, hook, pf, indev, outdev,
183181
sk, net, okfn);
184182

185-
ret = nf_hook_slow(skb, &state);
183+
ret = nf_hook_slow(skb, &state, hook_head);
186184
}
187185
rcu_read_unlock();
188186

‎include/linux/netfilter_ingress.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ static inline int nf_hook_ingress(struct sk_buff *skb)
2626
if (unlikely(!e))
2727
return 0;
2828

29-
nf_hook_state_init(&state, e, NF_NETDEV_INGRESS,
29+
nf_hook_state_init(&state, NF_NETDEV_INGRESS,
3030
NFPROTO_NETDEV, skb->dev, NULL, NULL,
3131
dev_net(skb->dev), NULL);
32-
return nf_hook_slow(skb, &state);
32+
return nf_hook_slow(skb, &state, e);
3333
}
3434

3535
static inline void nf_hook_ingress_init(struct net_device *dev)

‎include/net/netfilter/nf_queue.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct nf_queue_entry {
1212
unsigned int id;
1313

1414
struct nf_hook_state state;
15+
struct nf_hook_entry *hook;
1516
u16 size; /* sizeof(entry) + saved route keys */
1617

1718
/* extra space to store route keys */

‎net/bridge/br_netfilter_hooks.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,10 +1018,10 @@ int br_nf_hook_thresh(unsigned int hook, struct net *net,
10181018

10191019
/* We may already have this, but read-locks nest anyway */
10201020
rcu_read_lock();
1021-
nf_hook_state_init(&state, elem, hook, NFPROTO_BRIDGE, indev, outdev,
1021+
nf_hook_state_init(&state, hook, NFPROTO_BRIDGE, indev, outdev,
10221022
sk, net, okfn);
10231023

1024-
ret = nf_hook_slow(skb, &state);
1024+
ret = nf_hook_slow(skb, &state, elem);
10251025
rcu_read_unlock();
10261026
if (ret == 1)
10271027
ret = okfn(net, sk, skb);

‎net/bridge/netfilter/ebtable_broute.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static int ebt_broute(struct sk_buff *skb)
5353
struct nf_hook_state state;
5454
int ret;
5555

56-
nf_hook_state_init(&state, NULL, NF_BR_BROUTING,
56+
nf_hook_state_init(&state, NF_BR_BROUTING,
5757
NFPROTO_BRIDGE, skb->dev, NULL, NULL,
5858
dev_net(skb->dev), NULL);
5959

‎net/netfilter/core.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ unsigned int nf_iterate(struct sk_buff *skb,
308308
{
309309
unsigned int verdict;
310310

311-
while (*entryp) {
311+
do {
312312
repeat:
313313
verdict = (*entryp)->ops.hook((*entryp)->ops.priv, skb, state);
314314
if (verdict != NF_ACCEPT) {
@@ -317,20 +317,19 @@ unsigned int nf_iterate(struct sk_buff *skb,
317317
goto repeat;
318318
}
319319
*entryp = rcu_dereference((*entryp)->next);
320-
}
320+
} while (*entryp);
321321
return NF_ACCEPT;
322322
}
323323

324324

325325
/* Returns 1 if okfn() needs to be executed by the caller,
326326
* -EPERM for NF_DROP, 0 otherwise. Caller must hold rcu_read_lock. */
327-
int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state)
327+
int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
328+
struct nf_hook_entry *entry)
328329
{
329-
struct nf_hook_entry *entry;
330330
unsigned int verdict;
331331
int ret;
332332

333-
entry = rcu_dereference(state->hook_entries);
334333
next_hook:
335334
verdict = nf_iterate(skb, state, &entry);
336335
switch (verdict & NF_VERDICT_MASK) {

‎net/netfilter/nf_queue.c‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void nf_queue_nf_hook_drop(struct net *net, const struct nf_hook_entry *entry)
108108
}
109109

110110
static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
111-
unsigned int queuenum)
111+
struct nf_hook_entry *hook_entry, unsigned int queuenum)
112112
{
113113
int status = -ENOENT;
114114
struct nf_queue_entry *entry = NULL;
@@ -136,6 +136,7 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
136136
*entry = (struct nf_queue_entry) {
137137
.skb = skb,
138138
.state = *state,
139+
.hook = hook_entry,
139140
.size = sizeof(*entry) + afinfo->route_key_size,
140141
};
141142

@@ -163,8 +164,7 @@ int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
163164
struct nf_hook_entry *entry = *entryp;
164165
int ret;
165166

166-
RCU_INIT_POINTER(state->hook_entries, entry);
167-
ret = __nf_queue(skb, state, verdict >> NF_VERDICT_QBITS);
167+
ret = __nf_queue(skb, state, entry, verdict >> NF_VERDICT_QBITS);
168168
if (ret < 0) {
169169
if (ret == -ESRCH &&
170170
(verdict & NF_VERDICT_FLAG_QUEUE_BYPASS)) {
@@ -179,15 +179,12 @@ int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
179179

180180
void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
181181
{
182-
struct nf_hook_entry *hook_entry;
182+
struct nf_hook_entry *hook_entry = entry->hook;
183+
struct nf_hook_ops *elem = &hook_entry->ops;
183184
struct sk_buff *skb = entry->skb;
184185
const struct nf_afinfo *afinfo;
185-
struct nf_hook_ops *elem;
186186
int err;
187187

188-
hook_entry = rcu_dereference(entry->state.hook_entries);
189-
elem = &hook_entry->ops;
190-
191188
nf_queue_entry_release_refs(entry);
192189

193190
/* Continue traversal iff userspace said ok... */

‎net/netfilter/nfnetlink_queue.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ static struct notifier_block nfqnl_dev_notifier = {
919919

920920
static int nf_hook_cmp(struct nf_queue_entry *entry, unsigned long entry_ptr)
921921
{
922-
return rcu_access_pointer(entry->state.hook_entries) ==
922+
return rcu_access_pointer(entry->hook) ==
923923
(struct nf_hook_entry *)entry_ptr;
924924
}
925925

0 commit comments

Comments
 (0)