Skip to content

Commit 7be54ca

Browse files
committed
netfilter: nf_ct_ftp: add sequence tracking pickup facility for injected entries
This patch allows the FTP helper to pickup the sequence tracking from the first packet seen. This is useful to fix the breakage of the first FTP command after the failover while using conntrackd to synchronize states. The seq_aft_nl_num field in struct nf_ct_ftp_info has been shrinked to 16-bits (enough for what it does), so we can use the remaining 16-bits to store the flags while using the same size for the private FTP helper data. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 54eb3df commit 7be54ca

4 files changed

Lines changed: 31 additions & 3 deletions

File tree

‎include/linux/netfilter/nf_conntrack_ftp.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ enum nf_ct_ftp_type {
1818

1919
#define FTP_PORT 21
2020

21+
#define NF_CT_FTP_SEQ_PICKUP (1 << 0)
22+
2123
#define NUM_SEQ_TO_REMEMBER 2
2224
/* This structure exists only once per master */
2325
struct nf_ct_ftp_master {
2426
/* Valid seq positions for cmd matching after newline */
2527
u_int32_t seq_aft_nl[IP_CT_DIR_MAX][NUM_SEQ_TO_REMEMBER];
2628
/* 0 means seq_match_aft_nl not set */
27-
int seq_aft_nl_num[IP_CT_DIR_MAX];
29+
u_int16_t seq_aft_nl_num[IP_CT_DIR_MAX];
30+
/* pickup sequence tracking, useful for conntrackd */
31+
u_int16_t flags[IP_CT_DIR_MAX];
2832
};
2933

3034
struct nf_conntrack_expect;

‎net/netfilter/nf_conntrack_ftp.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,12 @@ static int help(struct sk_buff *skb,
396396

397397
/* Look up to see if we're just after a \n. */
398398
if (!find_nl_seq(ntohl(th->seq), ct_ftp_info, dir)) {
399+
/* We're picking up this, clear flags and let it continue */
400+
if (unlikely(ct_ftp_info->flags[dir] & NF_CT_FTP_SEQ_PICKUP)) {
401+
ct_ftp_info->flags[dir] ^= NF_CT_FTP_SEQ_PICKUP;
402+
goto skip_nl_seq;
403+
}
404+
399405
/* Now if this ends in \n, update ftp info. */
400406
pr_debug("nf_conntrack_ftp: wrong seq pos %s(%u) or %s(%u)\n",
401407
ct_ftp_info->seq_aft_nl_num[dir] > 0 ? "" : "(UNSET)",
@@ -406,6 +412,7 @@ static int help(struct sk_buff *skb,
406412
goto out_update_nl;
407413
}
408414

415+
skip_nl_seq:
409416
/* Initialize IP/IPv6 addr to expected address (it's not mentioned
410417
in EPSV responses) */
411418
cmd.l3num = nf_ct_l3num(ct);
@@ -512,6 +519,19 @@ static int help(struct sk_buff *skb,
512519
return ret;
513520
}
514521

522+
static int nf_ct_ftp_from_nlattr(struct nlattr *attr, struct nf_conn *ct)
523+
{
524+
struct nf_ct_ftp_master *ftp = nfct_help_data(ct);
525+
526+
/* This conntrack has been injected from user-space, always pick up
527+
* sequence tracking. Otherwise, the first FTP command after the
528+
* failover breaks.
529+
*/
530+
ftp->flags[IP_CT_DIR_ORIGINAL] |= NF_CT_FTP_SEQ_PICKUP;
531+
ftp->flags[IP_CT_DIR_REPLY] |= NF_CT_FTP_SEQ_PICKUP;
532+
return 0;
533+
}
534+
515535
static struct nf_conntrack_helper ftp[MAX_PORTS][2] __read_mostly;
516536

517537
static const struct nf_conntrack_expect_policy ftp_exp_policy = {
@@ -561,6 +581,7 @@ static int __init nf_conntrack_ftp_init(void)
561581
ftp[i][j].expect_policy = &ftp_exp_policy;
562582
ftp[i][j].me = THIS_MODULE;
563583
ftp[i][j].help = help;
584+
ftp[i][j].from_nlattr = nf_ct_ftp_from_nlattr;
564585
if (ports[i] == FTP_PORT)
565586
sprintf(ftp[i][j].name, "ftp");
566587
else

‎net/netfilter/nf_conntrack_netlink.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
12381238
if (help) {
12391239
if (help->helper == helper) {
12401240
/* update private helper data if allowed. */
1241-
if (helper->from_nlattr && helpinfo)
1241+
if (helper->from_nlattr)
12421242
helper->from_nlattr(helpinfo, ct);
12431243
return 0;
12441244
} else
@@ -1467,7 +1467,7 @@ ctnetlink_create_conntrack(struct net *net, u16 zone,
14671467
goto err2;
14681468
}
14691469
/* set private helper data if allowed. */
1470-
if (helper->from_nlattr && helpinfo)
1470+
if (helper->from_nlattr)
14711471
helper->from_nlattr(helpinfo, ct);
14721472

14731473
/* not in hash table yet so not strictly necessary */

‎net/netfilter/nfnetlink_cthelper.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ nfnl_cthelper_from_nlattr(struct nlattr *attr, struct nf_conn *ct)
8585
{
8686
const struct nf_conn_help *help = nfct_help(ct);
8787

88+
if (attr == NULL)
89+
return -EINVAL;
90+
8891
if (help->helper->data_len == 0)
8992
return -EINVAL;
9093

0 commit comments

Comments
 (0)