Skip to content

Commit c779e84

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: conntrack: remove get_timeout() indirection
Not needed, we can have the l4trackers fetch it themselvs. Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 97e08ca commit c779e84

12 files changed

+94
-104
lines changed

include/net/netfilter/nf_conntrack_l4proto.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ struct nf_conntrack_l4proto {
4545
int (*packet)(struct nf_conn *ct,
4646
const struct sk_buff *skb,
4747
unsigned int dataoff,
48-
enum ip_conntrack_info ctinfo,
49-
unsigned int *timeouts);
48+
enum ip_conntrack_info ctinfo);
5049

5150
/* Called when a new connection for this protocol found;
5251
* returns TRUE if it's OK. If so, packet() called next. */
5352
bool (*new)(struct nf_conn *ct, const struct sk_buff *skb,
54-
unsigned int dataoff, unsigned int *timeouts);
53+
unsigned int dataoff);
5554

5655
/* Called when a conntrack entry is destroyed */
5756
void (*destroy)(struct nf_conn *ct);
@@ -63,9 +62,6 @@ struct nf_conntrack_l4proto {
6362
/* called by gc worker if table is full */
6463
bool (*can_early_drop)(const struct nf_conn *ct);
6564

66-
/* Return the array of timeouts for this protocol. */
67-
unsigned int *(*get_timeouts)(struct net *net);
68-
6965
/* convert protoinfo to nfnetink attributes */
7066
int (*to_nlattr)(struct sk_buff *skb, struct nlattr *nla,
7167
struct nf_conn *ct);

include/net/netfilter/nf_conntrack_timeout.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,17 @@ struct nf_conn_timeout *nf_ct_timeout_ext_add(struct nf_conn *ct,
6767
#endif
6868
};
6969

70-
static inline unsigned int *
71-
nf_ct_timeout_lookup(struct net *net, struct nf_conn *ct,
72-
const struct nf_conntrack_l4proto *l4proto)
70+
static inline unsigned int *nf_ct_timeout_lookup(const struct nf_conn *ct)
7371
{
72+
unsigned int *timeouts = NULL;
7473
#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
7574
struct nf_conn_timeout *timeout_ext;
76-
unsigned int *timeouts;
7775

7876
timeout_ext = nf_ct_timeout_find(ct);
79-
if (timeout_ext) {
77+
if (timeout_ext)
8078
timeouts = nf_ct_timeout_data(timeout_ext);
81-
if (unlikely(!timeouts))
82-
timeouts = l4proto->get_timeouts(net);
83-
} else {
84-
timeouts = l4proto->get_timeouts(net);
85-
}
86-
87-
return timeouts;
88-
#else
89-
return l4proto->get_timeouts(net);
9079
#endif
80+
return timeouts;
9181
}
9282

9383
#ifdef CONFIG_NF_CONNTRACK_TIMEOUT

net/ipv4/netfilter/nf_conntrack_proto_icmp.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <net/netfilter/nf_conntrack_tuple.h>
2020
#include <net/netfilter/nf_conntrack_l4proto.h>
2121
#include <net/netfilter/nf_conntrack_core.h>
22+
#include <net/netfilter/nf_conntrack_timeout.h>
2223
#include <net/netfilter/nf_conntrack_zones.h>
2324
#include <net/netfilter/nf_log.h>
2425

@@ -80,20 +81,24 @@ static unsigned int *icmp_get_timeouts(struct net *net)
8081
static int icmp_packet(struct nf_conn *ct,
8182
const struct sk_buff *skb,
8283
unsigned int dataoff,
83-
enum ip_conntrack_info ctinfo,
84-
unsigned int *timeout)
84+
enum ip_conntrack_info ctinfo)
8585
{
8686
/* Do not immediately delete the connection after the first
8787
successful reply to avoid excessive conntrackd traffic
8888
and also to handle correctly ICMP echo reply duplicates. */
89+
unsigned int *timeout = nf_ct_timeout_lookup(ct);
90+
91+
if (!timeout)
92+
timeout = icmp_get_timeouts(nf_ct_net(ct));
93+
8994
nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
9095

9196
return NF_ACCEPT;
9297
}
9398

9499
/* Called when a new connection for this protocol found. */
95100
static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
96-
unsigned int dataoff, unsigned int *timeouts)
101+
unsigned int dataoff)
97102
{
98103
static const u_int8_t valid_new[] = {
99104
[ICMP_ECHO] = 1,
@@ -280,9 +285,11 @@ static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
280285
struct nf_icmp_net *in = icmp_pernet(net);
281286

282287
if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
288+
if (!timeout)
289+
timeout = &in->timeout;
283290
*timeout =
284291
ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
285-
} else {
292+
} else if (timeout) {
286293
/* Set default ICMP timeout. */
287294
*timeout = in->timeout;
288295
}
@@ -357,7 +364,6 @@ const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
357364
.pkt_to_tuple = icmp_pkt_to_tuple,
358365
.invert_tuple = icmp_invert_tuple,
359366
.packet = icmp_packet,
360-
.get_timeouts = icmp_get_timeouts,
361367
.new = icmp_new,
362368
.error = icmp_error,
363369
.destroy = NULL,

net/ipv6/netfilter/nf_conntrack_proto_icmpv6.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <net/netfilter/nf_conntrack_tuple.h>
2424
#include <net/netfilter/nf_conntrack_l4proto.h>
2525
#include <net/netfilter/nf_conntrack_core.h>
26+
#include <net/netfilter/nf_conntrack_timeout.h>
2627
#include <net/netfilter/nf_conntrack_zones.h>
2728
#include <net/netfilter/ipv6/nf_conntrack_icmpv6.h>
2829
#include <net/netfilter/nf_log.h>
@@ -93,9 +94,13 @@ static unsigned int *icmpv6_get_timeouts(struct net *net)
9394
static int icmpv6_packet(struct nf_conn *ct,
9495
const struct sk_buff *skb,
9596
unsigned int dataoff,
96-
enum ip_conntrack_info ctinfo,
97-
unsigned int *timeout)
97+
enum ip_conntrack_info ctinfo)
9898
{
99+
unsigned int *timeout = nf_ct_timeout_lookup(ct);
100+
101+
if (!timeout)
102+
timeout = icmpv6_get_timeouts(nf_ct_net(ct));
103+
99104
/* Do not immediately delete the connection after the first
100105
successful reply to avoid excessive conntrackd traffic
101106
and also to handle correctly ICMP echo reply duplicates. */
@@ -106,7 +111,7 @@ static int icmpv6_packet(struct nf_conn *ct,
106111

107112
/* Called when a new connection for this protocol found. */
108113
static bool icmpv6_new(struct nf_conn *ct, const struct sk_buff *skb,
109-
unsigned int dataoff, unsigned int *timeouts)
114+
unsigned int dataoff)
110115
{
111116
static const u_int8_t valid_new[] = {
112117
[ICMPV6_ECHO_REQUEST - 128] = 1,
@@ -280,6 +285,8 @@ static int icmpv6_timeout_nlattr_to_obj(struct nlattr *tb[],
280285
unsigned int *timeout = data;
281286
struct nf_icmp_net *in = icmpv6_pernet(net);
282287

288+
if (!timeout)
289+
timeout = icmpv6_get_timeouts(net);
283290
if (tb[CTA_TIMEOUT_ICMPV6_TIMEOUT]) {
284291
*timeout =
285292
ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMPV6_TIMEOUT])) * HZ;
@@ -358,7 +365,6 @@ const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6 =
358365
.pkt_to_tuple = icmpv6_pkt_to_tuple,
359366
.invert_tuple = icmpv6_invert_tuple,
360367
.packet = icmpv6_packet,
361-
.get_timeouts = icmpv6_get_timeouts,
362368
.new = icmpv6_new,
363369
.error = icmpv6_error,
364370
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)

net/netfilter/nf_conntrack_core.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,6 @@ init_conntrack(struct net *net, struct nf_conn *tmpl,
13371337
const struct nf_conntrack_zone *zone;
13381338
struct nf_conn_timeout *timeout_ext;
13391339
struct nf_conntrack_zone tmp;
1340-
unsigned int *timeouts;
13411340

13421341
if (!nf_ct_invert_tuple(&repl_tuple, tuple, l4proto)) {
13431342
pr_debug("Can't invert tuple.\n");
@@ -1356,15 +1355,8 @@ init_conntrack(struct net *net, struct nf_conn *tmpl,
13561355
}
13571356

13581357
timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL;
1359-
if (timeout_ext) {
1360-
timeouts = nf_ct_timeout_data(timeout_ext);
1361-
if (unlikely(!timeouts))
1362-
timeouts = l4proto->get_timeouts(net);
1363-
} else {
1364-
timeouts = l4proto->get_timeouts(net);
1365-
}
13661358

1367-
if (!l4proto->new(ct, skb, dataoff, timeouts)) {
1359+
if (!l4proto->new(ct, skb, dataoff)) {
13681360
nf_conntrack_free(ct);
13691361
pr_debug("can't track with proto module\n");
13701362
return NULL;
@@ -1493,7 +1485,6 @@ nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
14931485
const struct nf_conntrack_l4proto *l4proto;
14941486
struct nf_conn *ct, *tmpl;
14951487
enum ip_conntrack_info ctinfo;
1496-
unsigned int *timeouts;
14971488
u_int8_t protonum;
14981489
int dataoff, ret;
14991490

@@ -1552,10 +1543,7 @@ nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
15521543
goto out;
15531544
}
15541545

1555-
/* Decide what timeout policy we want to apply to this flow. */
1556-
timeouts = nf_ct_timeout_lookup(net, ct, l4proto);
1557-
1558-
ret = l4proto->packet(ct, skb, dataoff, ctinfo, timeouts);
1546+
ret = l4proto->packet(ct, skb, dataoff, ctinfo);
15591547
if (ret <= 0) {
15601548
/* Invalid: inverse of the return code tells
15611549
* the netfilter core what to do */

net/netfilter/nf_conntrack_proto_dccp.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <net/netfilter/nf_conntrack.h>
2424
#include <net/netfilter/nf_conntrack_l4proto.h>
2525
#include <net/netfilter/nf_conntrack_ecache.h>
26+
#include <net/netfilter/nf_conntrack_timeout.h>
2627
#include <net/netfilter/nf_log.h>
2728

2829
/* Timeouts are based on values from RFC4340:
@@ -389,7 +390,7 @@ static inline struct nf_dccp_net *dccp_pernet(struct net *net)
389390
}
390391

391392
static bool dccp_new(struct nf_conn *ct, const struct sk_buff *skb,
392-
unsigned int dataoff, unsigned int *timeouts)
393+
unsigned int dataoff)
393394
{
394395
struct net *net = nf_ct_net(ct);
395396
struct nf_dccp_net *dn;
@@ -437,19 +438,14 @@ static u64 dccp_ack_seq(const struct dccp_hdr *dh)
437438
ntohl(dhack->dccph_ack_nr_low);
438439
}
439440

440-
static unsigned int *dccp_get_timeouts(struct net *net)
441-
{
442-
return dccp_pernet(net)->dccp_timeout;
443-
}
444-
445441
static int dccp_packet(struct nf_conn *ct, const struct sk_buff *skb,
446-
unsigned int dataoff, enum ip_conntrack_info ctinfo,
447-
unsigned int *timeouts)
442+
unsigned int dataoff, enum ip_conntrack_info ctinfo)
448443
{
449444
enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
450445
struct dccp_hdr _dh, *dh;
451446
u_int8_t type, old_state, new_state;
452447
enum ct_dccp_roles role;
448+
unsigned int *timeouts;
453449

454450
dh = skb_header_pointer(skb, dataoff, sizeof(_dh), &_dh);
455451
BUG_ON(dh == NULL);
@@ -523,6 +519,9 @@ static int dccp_packet(struct nf_conn *ct, const struct sk_buff *skb,
523519
if (new_state != old_state)
524520
nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
525521

522+
timeouts = nf_ct_timeout_lookup(ct);
523+
if (!timeouts)
524+
timeouts = dccp_pernet(nf_ct_net(ct))->dccp_timeout;
526525
nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
527526

528527
return NF_ACCEPT;
@@ -843,7 +842,6 @@ const struct nf_conntrack_l4proto nf_conntrack_l4proto_dccp4 = {
843842
.l4proto = IPPROTO_DCCP,
844843
.new = dccp_new,
845844
.packet = dccp_packet,
846-
.get_timeouts = dccp_get_timeouts,
847845
.error = dccp_error,
848846
.can_early_drop = dccp_can_early_drop,
849847
#ifdef CONFIG_NF_CONNTRACK_PROCFS
@@ -877,7 +875,6 @@ const struct nf_conntrack_l4proto nf_conntrack_l4proto_dccp6 = {
877875
.l4proto = IPPROTO_DCCP,
878876
.new = dccp_new,
879877
.packet = dccp_packet,
880-
.get_timeouts = dccp_get_timeouts,
881878
.error = dccp_error,
882879
.can_early_drop = dccp_can_early_drop,
883880
#ifdef CONFIG_NF_CONNTRACK_PROCFS

net/netfilter/nf_conntrack_proto_generic.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/timer.h>
1212
#include <linux/netfilter.h>
1313
#include <net/netfilter/nf_conntrack_l4proto.h>
14+
#include <net/netfilter/nf_conntrack_timeout.h>
1415

1516
static const unsigned int nf_ct_generic_timeout = 600*HZ;
1617

@@ -41,25 +42,24 @@ static bool generic_pkt_to_tuple(const struct sk_buff *skb,
4142
return true;
4243
}
4344

44-
static unsigned int *generic_get_timeouts(struct net *net)
45-
{
46-
return &(generic_pernet(net)->timeout);
47-
}
48-
4945
/* Returns verdict for packet, or -1 for invalid. */
5046
static int generic_packet(struct nf_conn *ct,
5147
const struct sk_buff *skb,
5248
unsigned int dataoff,
53-
enum ip_conntrack_info ctinfo,
54-
unsigned int *timeout)
49+
enum ip_conntrack_info ctinfo)
5550
{
51+
const unsigned int *timeout = nf_ct_timeout_lookup(ct);
52+
53+
if (!timeout)
54+
timeout = &generic_pernet(nf_ct_net(ct))->timeout;
55+
5656
nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
5757
return NF_ACCEPT;
5858
}
5959

6060
/* Called when a new connection for this protocol found. */
6161
static bool generic_new(struct nf_conn *ct, const struct sk_buff *skb,
62-
unsigned int dataoff, unsigned int *timeouts)
62+
unsigned int dataoff)
6363
{
6464
bool ret;
6565

@@ -78,8 +78,11 @@ static bool generic_new(struct nf_conn *ct, const struct sk_buff *skb,
7878
static int generic_timeout_nlattr_to_obj(struct nlattr *tb[],
7979
struct net *net, void *data)
8080
{
81-
unsigned int *timeout = data;
8281
struct nf_generic_net *gn = generic_pernet(net);
82+
unsigned int *timeout = data;
83+
84+
if (!timeout)
85+
timeout = &gn->timeout;
8386

8487
if (tb[CTA_TIMEOUT_GENERIC_TIMEOUT])
8588
*timeout =
@@ -160,7 +163,6 @@ const struct nf_conntrack_l4proto nf_conntrack_l4proto_generic =
160163
.l4proto = 255,
161164
.pkt_to_tuple = generic_pkt_to_tuple,
162165
.packet = generic_packet,
163-
.get_timeouts = generic_get_timeouts,
164166
.new = generic_new,
165167
#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
166168
.ctnl_timeout = {

net/netfilter/nf_conntrack_proto_gre.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <net/netfilter/nf_conntrack_l4proto.h>
4040
#include <net/netfilter/nf_conntrack_helper.h>
4141
#include <net/netfilter/nf_conntrack_core.h>
42+
#include <net/netfilter/nf_conntrack_timeout.h>
4243
#include <linux/netfilter/nf_conntrack_proto_gre.h>
4344
#include <linux/netfilter/nf_conntrack_pptp.h>
4445

@@ -234,8 +235,7 @@ static unsigned int *gre_get_timeouts(struct net *net)
234235
static int gre_packet(struct nf_conn *ct,
235236
const struct sk_buff *skb,
236237
unsigned int dataoff,
237-
enum ip_conntrack_info ctinfo,
238-
unsigned int *timeouts)
238+
enum ip_conntrack_info ctinfo)
239239
{
240240
/* If we've seen traffic both ways, this is a GRE connection.
241241
* Extend timeout. */
@@ -254,8 +254,13 @@ static int gre_packet(struct nf_conn *ct,
254254

255255
/* Called when a new connection for this protocol found. */
256256
static bool gre_new(struct nf_conn *ct, const struct sk_buff *skb,
257-
unsigned int dataoff, unsigned int *timeouts)
257+
unsigned int dataoff)
258258
{
259+
unsigned int *timeouts = nf_ct_timeout_lookup(ct);
260+
261+
if (!timeouts)
262+
timeouts = gre_get_timeouts(nf_ct_net(ct));
263+
259264
pr_debug(": ");
260265
nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
261266

@@ -291,6 +296,8 @@ static int gre_timeout_nlattr_to_obj(struct nlattr *tb[],
291296
unsigned int *timeouts = data;
292297
struct netns_proto_gre *net_gre = gre_pernet(net);
293298

299+
if (!timeouts)
300+
timeouts = gre_get_timeouts(net);
294301
/* set default timeouts for GRE. */
295302
timeouts[GRE_CT_UNREPLIED] = net_gre->gre_timeouts[GRE_CT_UNREPLIED];
296303
timeouts[GRE_CT_REPLIED] = net_gre->gre_timeouts[GRE_CT_REPLIED];
@@ -350,7 +357,6 @@ static const struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 = {
350357
#ifdef CONFIG_NF_CONNTRACK_PROCFS
351358
.print_conntrack = gre_print_conntrack,
352359
#endif
353-
.get_timeouts = gre_get_timeouts,
354360
.packet = gre_packet,
355361
.new = gre_new,
356362
.destroy = gre_destroy,

0 commit comments

Comments
 (0)