Skip to content

Commit b20ab9c

Browse files
committed
netfilter: nf_ct_helper: better logging for dropped packets
Connection tracking helpers have to drop packets under exceptional situations. Currently, the user gets the following logging message in case that happens: nf_ct_%s: dropping packet ... However, depending on the helper, there are different reasons why a packet can be dropped. This patch modifies the existing code to provide more specific error message in the scope of each helper to help users to debug the reason why the packet has been dropped, ie: nf_ct_%s: dropping packet: reason ... Thanks to Joe Perches for many formatting suggestions. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 3812432 commit b20ab9c

16 files changed

Lines changed: 164 additions & 65 deletions

‎include/net/netfilter/nf_conntrack_helper.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ struct nf_ct_helper_expectfn {
100100
void (*expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
101101
};
102102

103+
__printf(3,4)
104+
void nf_ct_helper_log(struct sk_buff *skb, const struct nf_conn *ct,
105+
const char *fmt, ...);
106+
103107
void nf_ct_helper_expectfn_register(struct nf_ct_helper_expectfn *n);
104108
void nf_ct_helper_expectfn_unregister(struct nf_ct_helper_expectfn *n);
105109
struct nf_ct_helper_expectfn *

‎net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ static unsigned int ipv4_helper(unsigned int hooknum,
100100
enum ip_conntrack_info ctinfo;
101101
const struct nf_conn_help *help;
102102
const struct nf_conntrack_helper *helper;
103-
unsigned int ret;
104103

105104
/* This is where we call the helper: as the packet goes out. */
106105
ct = nf_ct_get(skb, &ctinfo);
@@ -116,13 +115,8 @@ static unsigned int ipv4_helper(unsigned int hooknum,
116115
if (!helper)
117116
return NF_ACCEPT;
118117

119-
ret = helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb),
120-
ct, ctinfo);
121-
if (ret != NF_ACCEPT && (ret & NF_VERDICT_MASK) != NF_QUEUE) {
122-
nf_log_packet(NFPROTO_IPV4, hooknum, skb, in, out, NULL,
123-
"nf_ct_%s: dropping packet", helper->name);
124-
}
125-
return ret;
118+
return helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb),
119+
ct, ctinfo);
126120
}
127121

128122
static unsigned int ipv4_confirm(unsigned int hooknum,

‎net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ static unsigned int ipv6_helper(unsigned int hooknum,
104104
const struct nf_conn_help *help;
105105
const struct nf_conntrack_helper *helper;
106106
enum ip_conntrack_info ctinfo;
107-
unsigned int ret;
108107
__be16 frag_off;
109108
int protoff;
110109
u8 nexthdr;
@@ -130,12 +129,7 @@ static unsigned int ipv6_helper(unsigned int hooknum,
130129
return NF_ACCEPT;
131130
}
132131

133-
ret = helper->help(skb, protoff, ct, ctinfo);
134-
if (ret != NF_ACCEPT && (ret & NF_VERDICT_MASK) != NF_QUEUE) {
135-
nf_log_packet(NFPROTO_IPV6, hooknum, skb, in, out, NULL,
136-
"nf_ct_%s: dropping packet", helper->name);
137-
}
138-
return ret;
132+
return helper->help(skb, protoff, ct, ctinfo);
139133
}
140134

141135
static unsigned int ipv6_confirm(unsigned int hooknum,

‎net/netfilter/nf_conntrack_amanda.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ static int amanda_help(struct sk_buff *skb,
145145

146146
exp = nf_ct_expect_alloc(ct);
147147
if (exp == NULL) {
148+
nf_ct_helper_log(skb, ct, "cannot alloc expectation");
148149
ret = NF_DROP;
149150
goto out;
150151
}
@@ -158,8 +159,10 @@ static int amanda_help(struct sk_buff *skb,
158159
if (nf_nat_amanda && ct->status & IPS_NAT_MASK)
159160
ret = nf_nat_amanda(skb, ctinfo, protoff,
160161
off - dataoff, len, exp);
161-
else if (nf_ct_expect_related(exp) != 0)
162+
else if (nf_ct_expect_related(exp) != 0) {
163+
nf_ct_helper_log(skb, ct, "cannot add expectation");
162164
ret = NF_DROP;
165+
}
163166
nf_ct_expect_put(exp);
164167
}
165168

‎net/netfilter/nf_conntrack_ftp.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,8 @@ static int help(struct sk_buff *skb,
435435
connection tracking, not packet filtering.
436436
However, it is necessary for accurate tracking in
437437
this case. */
438-
pr_debug("conntrack_ftp: partial %s %u+%u\n",
439-
search[dir][i].pattern, ntohl(th->seq), datalen);
438+
nf_ct_helper_log(skb, ct, "partial matching of `%s'",
439+
search[dir][i].pattern);
440440
ret = NF_DROP;
441441
goto out;
442442
} else if (found == 0) { /* No match */
@@ -450,6 +450,7 @@ static int help(struct sk_buff *skb,
450450

451451
exp = nf_ct_expect_alloc(ct);
452452
if (exp == NULL) {
453+
nf_ct_helper_log(skb, ct, "cannot alloc expectation");
453454
ret = NF_DROP;
454455
goto out;
455456
}
@@ -500,9 +501,10 @@ static int help(struct sk_buff *skb,
500501
protoff, matchoff, matchlen, exp);
501502
else {
502503
/* Can't expect this? Best to drop packet now. */
503-
if (nf_ct_expect_related(exp) != 0)
504+
if (nf_ct_expect_related(exp) != 0) {
505+
nf_ct_helper_log(skb, ct, "cannot add expectation");
504506
ret = NF_DROP;
505-
else
507+
} else
506508
ret = NF_ACCEPT;
507509
}
508510

‎net/netfilter/nf_conntrack_h323_main.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ static int h245_help(struct sk_buff *skb, unsigned int protoff,
623623

624624
drop:
625625
spin_unlock_bh(&nf_h323_lock);
626-
net_info_ratelimited("nf_ct_h245: packet dropped\n");
626+
nf_ct_helper_log(skb, ct, "cannot process H.245 message");
627627
return NF_DROP;
628628
}
629629

@@ -1197,7 +1197,7 @@ static int q931_help(struct sk_buff *skb, unsigned int protoff,
11971197

11981198
drop:
11991199
spin_unlock_bh(&nf_h323_lock);
1200-
net_info_ratelimited("nf_ct_q931: packet dropped\n");
1200+
nf_ct_helper_log(skb, ct, "cannot process Q.931 message");
12011201
return NF_DROP;
12021202
}
12031203

@@ -1795,7 +1795,7 @@ static int ras_help(struct sk_buff *skb, unsigned int protoff,
17951795

17961796
drop:
17971797
spin_unlock_bh(&nf_h323_lock);
1798-
net_info_ratelimited("nf_ct_ras: packet dropped\n");
1798+
nf_ct_helper_log(skb, ct, "cannot process RAS message");
17991799
return NF_DROP;
18001800
}
18011801

‎net/netfilter/nf_conntrack_helper.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <net/netfilter/nf_conntrack_helper.h>
2929
#include <net/netfilter/nf_conntrack_core.h>
3030
#include <net/netfilter/nf_conntrack_extend.h>
31+
#include <net/netfilter/nf_log.h>
3132

3233
static DEFINE_MUTEX(nf_ct_helper_mutex);
3334
struct hlist_head *nf_ct_helper_hash __read_mostly;
@@ -332,6 +333,24 @@ nf_ct_helper_expectfn_find_by_symbol(const void *symbol)
332333
}
333334
EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_symbol);
334335

336+
__printf(3, 4)
337+
void nf_ct_helper_log(struct sk_buff *skb, const struct nf_conn *ct,
338+
const char *fmt, ...)
339+
{
340+
const struct nf_conn_help *help;
341+
const struct nf_conntrack_helper *helper;
342+
343+
/* Called from the helper function, this call never fails */
344+
help = nfct_help(ct);
345+
346+
/* rcu_read_lock()ed by nf_hook_slow */
347+
helper = rcu_dereference(help->helper);
348+
349+
nf_log_packet(nf_ct_l3num(ct), 0, skb, NULL, NULL, NULL,
350+
"nf_ct_%s: dropping packet: %s ", helper->name, fmt);
351+
}
352+
EXPORT_SYMBOL_GPL(nf_ct_helper_log);
353+
335354
int nf_conntrack_helper_register(struct nf_conntrack_helper *me)
336355
{
337356
int ret = 0;

‎net/netfilter/nf_conntrack_irc.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ static int help(struct sk_buff *skb, unsigned int protoff,
194194

195195
exp = nf_ct_expect_alloc(ct);
196196
if (exp == NULL) {
197+
nf_ct_helper_log(skb, ct,
198+
"cannot alloc expectation");
197199
ret = NF_DROP;
198200
goto out;
199201
}
@@ -210,8 +212,11 @@ static int help(struct sk_buff *skb, unsigned int protoff,
210212
addr_beg_p - ib_ptr,
211213
addr_end_p - addr_beg_p,
212214
exp);
213-
else if (nf_ct_expect_related(exp) != 0)
215+
else if (nf_ct_expect_related(exp) != 0) {
216+
nf_ct_helper_log(skb, ct,
217+
"cannot add expectation");
214218
ret = NF_DROP;
219+
}
215220
nf_ct_expect_put(exp);
216221
goto out;
217222
}

‎net/netfilter/nf_conntrack_sane.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ static int help(struct sk_buff *skb,
138138

139139
exp = nf_ct_expect_alloc(ct);
140140
if (exp == NULL) {
141+
nf_ct_helper_log(skb, ct, "cannot alloc expectation");
141142
ret = NF_DROP;
142143
goto out;
143144
}
@@ -151,8 +152,10 @@ static int help(struct sk_buff *skb,
151152
nf_ct_dump_tuple(&exp->tuple);
152153

153154
/* Can't expect this? Best to drop packet now. */
154-
if (nf_ct_expect_related(exp) != 0)
155+
if (nf_ct_expect_related(exp) != 0) {
156+
nf_ct_helper_log(skb, ct, "cannot add expectation");
155157
ret = NF_DROP;
158+
}
156159

157160
nf_ct_expect_put(exp);
158161

0 commit comments

Comments
 (0)