Skip to content

Commit 368982c

Browse files
committed
netfilter: nfnetlink_queue: resolve clash for unconfirmed conntracks
In nfqueue, two consecutive skbuffs may race to create the conntrack entry. Hence, the one that loses the race gets dropped due to clash in the insertion into the hashes from the nf_conntrack_confirm() path. This patch adds a new nf_conntrack_update() function which searches for possible clashes and resolve them. NAT mangling for the packet losing race is corrected by using the conntrack information that won race. In order to avoid direct module dependencies with conntrack and NAT, the nf_ct_hook and nf_nat_hook structures are used for this purpose. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 2c205dd commit 368982c

4 files changed

Lines changed: 132 additions & 19 deletions

File tree

‎include/linux/netfilter.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,15 @@ int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry);
324324
struct nf_conn;
325325
enum nf_nat_manip_type;
326326
struct nlattr;
327+
enum ip_conntrack_dir;
327328

328329
struct nf_nat_hook {
329330
int (*parse_nat_setup)(struct nf_conn *ct, enum nf_nat_manip_type manip,
330331
const struct nlattr *attr);
331332
void (*decode_session)(struct sk_buff *skb, struct flowi *fl);
333+
unsigned int (*manip_pkt)(struct sk_buff *skb, struct nf_conn *ct,
334+
enum nf_nat_manip_type mtype,
335+
enum ip_conntrack_dir dir);
332336
};
333337

334338
extern struct nf_nat_hook __rcu *nf_nat_hook;
@@ -392,6 +396,7 @@ struct nf_conn;
392396
enum ip_conntrack_info;
393397

394398
struct nf_ct_hook {
399+
int (*update)(struct net *net, struct sk_buff *skb);
395400
void (*destroy)(struct nf_conntrack *);
396401
};
397402
extern struct nf_ct_hook __rcu *nf_ct_hook;

‎net/netfilter/nf_conntrack_core.c‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,82 @@ static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)
16071607
nf_conntrack_get(skb_nfct(nskb));
16081608
}
16091609

1610+
static int nf_conntrack_update(struct net *net, struct sk_buff *skb)
1611+
{
1612+
const struct nf_conntrack_l3proto *l3proto;
1613+
const struct nf_conntrack_l4proto *l4proto;
1614+
struct nf_conntrack_tuple_hash *h;
1615+
struct nf_conntrack_tuple tuple;
1616+
enum ip_conntrack_info ctinfo;
1617+
struct nf_nat_hook *nat_hook;
1618+
unsigned int dataoff, status;
1619+
struct nf_conn *ct;
1620+
u16 l3num;
1621+
u8 l4num;
1622+
1623+
ct = nf_ct_get(skb, &ctinfo);
1624+
if (!ct || nf_ct_is_confirmed(ct))
1625+
return 0;
1626+
1627+
l3num = nf_ct_l3num(ct);
1628+
l3proto = nf_ct_l3proto_find_get(l3num);
1629+
1630+
if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
1631+
&l4num) <= 0)
1632+
return -1;
1633+
1634+
l4proto = nf_ct_l4proto_find_get(l3num, l4num);
1635+
1636+
if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
1637+
l4num, net, &tuple, l3proto, l4proto))
1638+
return -1;
1639+
1640+
if (ct->status & IPS_SRC_NAT) {
1641+
memcpy(tuple.src.u3.all,
1642+
ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.all,
1643+
sizeof(tuple.src.u3.all));
1644+
tuple.src.u.all =
1645+
ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.all;
1646+
}
1647+
1648+
if (ct->status & IPS_DST_NAT) {
1649+
memcpy(tuple.dst.u3.all,
1650+
ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.all,
1651+
sizeof(tuple.dst.u3.all));
1652+
tuple.dst.u.all =
1653+
ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.all;
1654+
}
1655+
1656+
h = nf_conntrack_find_get(net, nf_ct_zone(ct), &tuple);
1657+
if (!h)
1658+
return 0;
1659+
1660+
/* Store status bits of the conntrack that is clashing to re-do NAT
1661+
* mangling according to what it has been done already to this packet.
1662+
*/
1663+
status = ct->status;
1664+
1665+
nf_ct_put(ct);
1666+
ct = nf_ct_tuplehash_to_ctrack(h);
1667+
nf_ct_set(skb, ct, ctinfo);
1668+
1669+
nat_hook = rcu_dereference(nf_nat_hook);
1670+
if (!nat_hook)
1671+
return 0;
1672+
1673+
if (status & IPS_SRC_NAT &&
1674+
nat_hook->manip_pkt(skb, ct, NF_NAT_MANIP_SRC,
1675+
IP_CT_DIR_ORIGINAL) == NF_DROP)
1676+
return -1;
1677+
1678+
if (status & IPS_DST_NAT &&
1679+
nat_hook->manip_pkt(skb, ct, NF_NAT_MANIP_DST,
1680+
IP_CT_DIR_ORIGINAL) == NF_DROP)
1681+
return -1;
1682+
1683+
return 0;
1684+
}
1685+
16101686
/* Bring out ya dead! */
16111687
static struct nf_conn *
16121688
get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
@@ -2126,6 +2202,7 @@ int nf_conntrack_init_start(void)
21262202
}
21272203

21282204
static struct nf_ct_hook nf_conntrack_hook = {
2205+
.update = nf_conntrack_update,
21292206
.destroy = destroy_conntrack,
21302207
};
21312208

‎net/netfilter/nf_nat_core.c‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -493,17 +493,36 @@ nf_nat_alloc_null_binding(struct nf_conn *ct, unsigned int hooknum)
493493
}
494494
EXPORT_SYMBOL_GPL(nf_nat_alloc_null_binding);
495495

496+
static unsigned int nf_nat_manip_pkt(struct sk_buff *skb, struct nf_conn *ct,
497+
enum nf_nat_manip_type mtype,
498+
enum ip_conntrack_dir dir)
499+
{
500+
const struct nf_nat_l3proto *l3proto;
501+
const struct nf_nat_l4proto *l4proto;
502+
struct nf_conntrack_tuple target;
503+
504+
/* We are aiming to look like inverse of other direction. */
505+
nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
506+
507+
l3proto = __nf_nat_l3proto_find(target.src.l3num);
508+
l4proto = __nf_nat_l4proto_find(target.src.l3num,
509+
target.dst.protonum);
510+
if (!l3proto->manip_pkt(skb, 0, l4proto, &target, mtype))
511+
return NF_DROP;
512+
513+
return NF_ACCEPT;
514+
}
515+
496516
/* Do packet manipulations according to nf_nat_setup_info. */
497517
unsigned int nf_nat_packet(struct nf_conn *ct,
498518
enum ip_conntrack_info ctinfo,
499519
unsigned int hooknum,
500520
struct sk_buff *skb)
501521
{
502-
const struct nf_nat_l3proto *l3proto;
503-
const struct nf_nat_l4proto *l4proto;
522+
enum nf_nat_manip_type mtype = HOOK2MANIP(hooknum);
504523
enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
524+
unsigned int verdict = NF_ACCEPT;
505525
unsigned long statusbit;
506-
enum nf_nat_manip_type mtype = HOOK2MANIP(hooknum);
507526

508527
if (mtype == NF_NAT_MANIP_SRC)
509528
statusbit = IPS_SRC_NAT;
@@ -515,19 +534,10 @@ unsigned int nf_nat_packet(struct nf_conn *ct,
515534
statusbit ^= IPS_NAT_MASK;
516535

517536
/* Non-atomic: these bits don't change. */
518-
if (ct->status & statusbit) {
519-
struct nf_conntrack_tuple target;
520-
521-
/* We are aiming to look like inverse of other direction. */
522-
nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
537+
if (ct->status & statusbit)
538+
verdict = nf_nat_manip_pkt(skb, ct, mtype, dir);
523539

524-
l3proto = __nf_nat_l3proto_find(target.src.l3num);
525-
l4proto = __nf_nat_l4proto_find(target.src.l3num,
526-
target.dst.protonum);
527-
if (!l3proto->manip_pkt(skb, 0, l4proto, &target, mtype))
528-
return NF_DROP;
529-
}
530-
return NF_ACCEPT;
540+
return verdict;
531541
}
532542
EXPORT_SYMBOL_GPL(nf_nat_packet);
533543

@@ -1031,6 +1041,7 @@ struct nf_nat_hook nat_hook = {
10311041
#ifdef CONFIG_XFRM
10321042
.decode_session = __nf_nat_decode_session,
10331043
#endif
1044+
.manip_pkt = nf_nat_manip_pkt,
10341045
};
10351046

10361047
static int __init nf_nat_init(void)

‎net/netfilter/nfnetlink_queue.c‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,25 @@ find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
227227
return entry;
228228
}
229229

230+
static void nfqnl_reinject(struct nf_queue_entry *entry, unsigned int verdict)
231+
{
232+
struct nf_ct_hook *ct_hook;
233+
int err;
234+
235+
if (verdict == NF_ACCEPT ||
236+
verdict == NF_STOP) {
237+
rcu_read_lock();
238+
ct_hook = rcu_dereference(nf_ct_hook);
239+
if (ct_hook) {
240+
err = ct_hook->update(entry->state.net, entry->skb);
241+
if (err < 0)
242+
verdict = NF_DROP;
243+
}
244+
rcu_read_unlock();
245+
}
246+
nf_reinject(entry, verdict);
247+
}
248+
230249
static void
231250
nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
232251
{
@@ -237,7 +256,7 @@ nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
237256
if (!cmpfn || cmpfn(entry, data)) {
238257
list_del(&entry->list);
239258
queue->queue_total--;
240-
nf_reinject(entry, NF_DROP);
259+
nfqnl_reinject(entry, NF_DROP);
241260
}
242261
}
243262
spin_unlock_bh(&queue->lock);
@@ -686,7 +705,7 @@ __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
686705
err_out_unlock:
687706
spin_unlock_bh(&queue->lock);
688707
if (failopen)
689-
nf_reinject(entry, NF_ACCEPT);
708+
nfqnl_reinject(entry, NF_ACCEPT);
690709
err_out:
691710
return err;
692711
}
@@ -1085,7 +1104,8 @@ static int nfqnl_recv_verdict_batch(struct net *net, struct sock *ctnl,
10851104
list_for_each_entry_safe(entry, tmp, &batch_list, list) {
10861105
if (nfqa[NFQA_MARK])
10871106
entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
1088-
nf_reinject(entry, verdict);
1107+
1108+
nfqnl_reinject(entry, verdict);
10891109
}
10901110
return 0;
10911111
}
@@ -1208,7 +1228,7 @@ static int nfqnl_recv_verdict(struct net *net, struct sock *ctnl,
12081228
if (nfqa[NFQA_MARK])
12091229
entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
12101230

1211-
nf_reinject(entry, verdict);
1231+
nfqnl_reinject(entry, verdict);
12121232
return 0;
12131233
}
12141234

0 commit comments

Comments
 (0)