Skip to content

Commit

Permalink
netfilter: nf_ct_helper: better logging for dropped packets
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Pablo Neira Ayuso committed Feb 19, 2013
1 parent 3812432 commit b20ab9c
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 65 deletions.
4 changes: 4 additions & 0 deletions include/net/netfilter/nf_conntrack_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ struct nf_ct_helper_expectfn {
void (*expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
};

__printf(3,4)
void nf_ct_helper_log(struct sk_buff *skb, const struct nf_conn *ct,
const char *fmt, ...);

void nf_ct_helper_expectfn_register(struct nf_ct_helper_expectfn *n);
void nf_ct_helper_expectfn_unregister(struct nf_ct_helper_expectfn *n);
struct nf_ct_helper_expectfn *
Expand Down
10 changes: 2 additions & 8 deletions net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ static unsigned int ipv4_helper(unsigned int hooknum,
enum ip_conntrack_info ctinfo;
const struct nf_conn_help *help;
const struct nf_conntrack_helper *helper;
unsigned int ret;

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

ret = helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb),
ct, ctinfo);
if (ret != NF_ACCEPT && (ret & NF_VERDICT_MASK) != NF_QUEUE) {
nf_log_packet(NFPROTO_IPV4, hooknum, skb, in, out, NULL,
"nf_ct_%s: dropping packet", helper->name);
}
return ret;
return helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb),
ct, ctinfo);
}

static unsigned int ipv4_confirm(unsigned int hooknum,
Expand Down
8 changes: 1 addition & 7 deletions net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ static unsigned int ipv6_helper(unsigned int hooknum,
const struct nf_conn_help *help;
const struct nf_conntrack_helper *helper;
enum ip_conntrack_info ctinfo;
unsigned int ret;
__be16 frag_off;
int protoff;
u8 nexthdr;
Expand All @@ -130,12 +129,7 @@ static unsigned int ipv6_helper(unsigned int hooknum,
return NF_ACCEPT;
}

ret = helper->help(skb, protoff, ct, ctinfo);
if (ret != NF_ACCEPT && (ret & NF_VERDICT_MASK) != NF_QUEUE) {
nf_log_packet(NFPROTO_IPV6, hooknum, skb, in, out, NULL,
"nf_ct_%s: dropping packet", helper->name);
}
return ret;
return helper->help(skb, protoff, ct, ctinfo);
}

static unsigned int ipv6_confirm(unsigned int hooknum,
Expand Down
5 changes: 4 additions & 1 deletion net/netfilter/nf_conntrack_amanda.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ static int amanda_help(struct sk_buff *skb,

exp = nf_ct_expect_alloc(ct);
if (exp == NULL) {
nf_ct_helper_log(skb, ct, "cannot alloc expectation");
ret = NF_DROP;
goto out;
}
Expand All @@ -158,8 +159,10 @@ static int amanda_help(struct sk_buff *skb,
if (nf_nat_amanda && ct->status & IPS_NAT_MASK)
ret = nf_nat_amanda(skb, ctinfo, protoff,
off - dataoff, len, exp);
else if (nf_ct_expect_related(exp) != 0)
else if (nf_ct_expect_related(exp) != 0) {
nf_ct_helper_log(skb, ct, "cannot add expectation");
ret = NF_DROP;
}
nf_ct_expect_put(exp);
}

Expand Down
10 changes: 6 additions & 4 deletions net/netfilter/nf_conntrack_ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ static int help(struct sk_buff *skb,
connection tracking, not packet filtering.
However, it is necessary for accurate tracking in
this case. */
pr_debug("conntrack_ftp: partial %s %u+%u\n",
search[dir][i].pattern, ntohl(th->seq), datalen);
nf_ct_helper_log(skb, ct, "partial matching of `%s'",
search[dir][i].pattern);
ret = NF_DROP;
goto out;
} else if (found == 0) { /* No match */
Expand All @@ -450,6 +450,7 @@ static int help(struct sk_buff *skb,

exp = nf_ct_expect_alloc(ct);
if (exp == NULL) {
nf_ct_helper_log(skb, ct, "cannot alloc expectation");
ret = NF_DROP;
goto out;
}
Expand Down Expand Up @@ -500,9 +501,10 @@ static int help(struct sk_buff *skb,
protoff, matchoff, matchlen, exp);
else {
/* Can't expect this? Best to drop packet now. */
if (nf_ct_expect_related(exp) != 0)
if (nf_ct_expect_related(exp) != 0) {
nf_ct_helper_log(skb, ct, "cannot add expectation");
ret = NF_DROP;
else
} else
ret = NF_ACCEPT;
}

Expand Down
6 changes: 3 additions & 3 deletions net/netfilter/nf_conntrack_h323_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ static int h245_help(struct sk_buff *skb, unsigned int protoff,

drop:
spin_unlock_bh(&nf_h323_lock);
net_info_ratelimited("nf_ct_h245: packet dropped\n");
nf_ct_helper_log(skb, ct, "cannot process H.245 message");
return NF_DROP;
}

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

drop:
spin_unlock_bh(&nf_h323_lock);
net_info_ratelimited("nf_ct_q931: packet dropped\n");
nf_ct_helper_log(skb, ct, "cannot process Q.931 message");
return NF_DROP;
}

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

drop:
spin_unlock_bh(&nf_h323_lock);
net_info_ratelimited("nf_ct_ras: packet dropped\n");
nf_ct_helper_log(skb, ct, "cannot process RAS message");
return NF_DROP;
}

Expand Down
19 changes: 19 additions & 0 deletions net/netfilter/nf_conntrack_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <net/netfilter/nf_conntrack_helper.h>
#include <net/netfilter/nf_conntrack_core.h>
#include <net/netfilter/nf_conntrack_extend.h>
#include <net/netfilter/nf_log.h>

static DEFINE_MUTEX(nf_ct_helper_mutex);
struct hlist_head *nf_ct_helper_hash __read_mostly;
Expand Down Expand Up @@ -332,6 +333,24 @@ nf_ct_helper_expectfn_find_by_symbol(const void *symbol)
}
EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_symbol);

__printf(3, 4)
void nf_ct_helper_log(struct sk_buff *skb, const struct nf_conn *ct,
const char *fmt, ...)
{
const struct nf_conn_help *help;
const struct nf_conntrack_helper *helper;

/* Called from the helper function, this call never fails */
help = nfct_help(ct);

/* rcu_read_lock()ed by nf_hook_slow */
helper = rcu_dereference(help->helper);

nf_log_packet(nf_ct_l3num(ct), 0, skb, NULL, NULL, NULL,
"nf_ct_%s: dropping packet: %s ", helper->name, fmt);
}
EXPORT_SYMBOL_GPL(nf_ct_helper_log);

int nf_conntrack_helper_register(struct nf_conntrack_helper *me)
{
int ret = 0;
Expand Down
7 changes: 6 additions & 1 deletion net/netfilter/nf_conntrack_irc.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ static int help(struct sk_buff *skb, unsigned int protoff,

exp = nf_ct_expect_alloc(ct);
if (exp == NULL) {
nf_ct_helper_log(skb, ct,
"cannot alloc expectation");
ret = NF_DROP;
goto out;
}
Expand All @@ -210,8 +212,11 @@ static int help(struct sk_buff *skb, unsigned int protoff,
addr_beg_p - ib_ptr,
addr_end_p - addr_beg_p,
exp);
else if (nf_ct_expect_related(exp) != 0)
else if (nf_ct_expect_related(exp) != 0) {
nf_ct_helper_log(skb, ct,
"cannot add expectation");
ret = NF_DROP;
}
nf_ct_expect_put(exp);
goto out;
}
Expand Down
5 changes: 4 additions & 1 deletion net/netfilter/nf_conntrack_sane.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ static int help(struct sk_buff *skb,

exp = nf_ct_expect_alloc(ct);
if (exp == NULL) {
nf_ct_helper_log(skb, ct, "cannot alloc expectation");
ret = NF_DROP;
goto out;
}
Expand All @@ -151,8 +152,10 @@ static int help(struct sk_buff *skb,
nf_ct_dump_tuple(&exp->tuple);

/* Can't expect this? Best to drop packet now. */
if (nf_ct_expect_related(exp) != 0)
if (nf_ct_expect_related(exp) != 0) {
nf_ct_helper_log(skb, ct, "cannot add expectation");
ret = NF_DROP;
}

nf_ct_expect_put(exp);

Expand Down
Loading

0 comments on commit b20ab9c

Please sign in to comment.