Skip to content

Commit 6aafeef

Browse files
jpirkodavem330
authored andcommitted
netfilter: push reasm skb through instead of original frag skbs
Pushing original fragments through causes several problems. For example for matching, frags may not be matched correctly. Take following example: <example> On HOSTA do: ip6tables -I INPUT -p icmpv6 -j DROP ip6tables -I INPUT -p icmpv6 -m icmp6 --icmpv6-type 128 -j ACCEPT and on HOSTB you do: ping6 HOSTA -s2000 (MTU is 1500) Incoming echo requests will be filtered out on HOSTA. This issue does not occur with smaller packets than MTU (where fragmentation does not happen) </example> As was discussed previously, the only correct solution seems to be to use reassembled skb instead of separete frags. Doing this has positive side effects in reducing sk_buff by one pointer (nfct_reasm) and also the reams dances in ipvs and conntrack can be removed. Future plan is to remove net/ipv6/netfilter/nf_conntrack_reasm.c entirely and use code in net/ipv6/reassembly.c instead. Signed-off-by: Jiri Pirko <jiri@resnulli.us> Acked-by: Julian Anastasov <ja@ssi.bg> Signed-off-by: Marcelo Ricardo Leitner <mleitner@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 9037c35 commit 6aafeef

9 files changed

Lines changed: 13 additions & 203 deletions

File tree

‎include/linux/skbuff.h‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,6 @@ typedef unsigned int sk_buff_data_t;
337337
typedef unsigned char *sk_buff_data_t;
338338
#endif
339339

340-
#if defined(CONFIG_NF_DEFRAG_IPV4) || defined(CONFIG_NF_DEFRAG_IPV4_MODULE) || \
341-
defined(CONFIG_NF_DEFRAG_IPV6) || defined(CONFIG_NF_DEFRAG_IPV6_MODULE)
342-
#define NET_SKBUFF_NF_DEFRAG_NEEDED 1
343-
#endif
344-
345340
/**
346341
* struct sk_buff - socket buffer
347342
* @next: Next buffer in list
@@ -374,7 +369,6 @@ typedef unsigned char *sk_buff_data_t;
374369
* @protocol: Packet protocol from driver
375370
* @destructor: Destruct function
376371
* @nfct: Associated connection, if any
377-
* @nfct_reasm: netfilter conntrack re-assembly pointer
378372
* @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
379373
* @skb_iif: ifindex of device we arrived on
380374
* @tc_index: Traffic control index
@@ -463,9 +457,6 @@ struct sk_buff {
463457
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
464458
struct nf_conntrack *nfct;
465459
#endif
466-
#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
467-
struct sk_buff *nfct_reasm;
468-
#endif
469460
#ifdef CONFIG_BRIDGE_NETFILTER
470461
struct nf_bridge_info *nf_bridge;
471462
#endif
@@ -2595,18 +2586,6 @@ static inline void nf_conntrack_get(struct nf_conntrack *nfct)
25952586
atomic_inc(&nfct->use);
25962587
}
25972588
#endif
2598-
#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
2599-
static inline void nf_conntrack_get_reasm(struct sk_buff *skb)
2600-
{
2601-
if (skb)
2602-
atomic_inc(&skb->users);
2603-
}
2604-
static inline void nf_conntrack_put_reasm(struct sk_buff *skb)
2605-
{
2606-
if (skb)
2607-
kfree_skb(skb);
2608-
}
2609-
#endif
26102589
#ifdef CONFIG_BRIDGE_NETFILTER
26112590
static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
26122591
{
@@ -2625,10 +2604,6 @@ static inline void nf_reset(struct sk_buff *skb)
26252604
nf_conntrack_put(skb->nfct);
26262605
skb->nfct = NULL;
26272606
#endif
2628-
#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
2629-
nf_conntrack_put_reasm(skb->nfct_reasm);
2630-
skb->nfct_reasm = NULL;
2631-
#endif
26322607
#ifdef CONFIG_BRIDGE_NETFILTER
26332608
nf_bridge_put(skb->nf_bridge);
26342609
skb->nf_bridge = NULL;
@@ -2650,10 +2625,6 @@ static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src)
26502625
nf_conntrack_get(src->nfct);
26512626
dst->nfctinfo = src->nfctinfo;
26522627
#endif
2653-
#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
2654-
dst->nfct_reasm = src->nfct_reasm;
2655-
nf_conntrack_get_reasm(src->nfct_reasm);
2656-
#endif
26572628
#ifdef CONFIG_BRIDGE_NETFILTER
26582629
dst->nf_bridge = src->nf_bridge;
26592630
nf_bridge_get(src->nf_bridge);
@@ -2665,9 +2636,6 @@ static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
26652636
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
26662637
nf_conntrack_put(dst->nfct);
26672638
#endif
2668-
#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
2669-
nf_conntrack_put_reasm(dst->nfct_reasm);
2670-
#endif
26712639
#ifdef CONFIG_BRIDGE_NETFILTER
26722640
nf_bridge_put(dst->nf_bridge);
26732641
#endif

‎include/net/ip_vs.h‎

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -109,42 +109,19 @@ extern int ip_vs_conn_tab_size;
109109
struct ip_vs_iphdr {
110110
__u32 len; /* IPv4 simply where L4 starts
111111
IPv6 where L4 Transport Header starts */
112-
__u32 thoff_reasm; /* Transport Header Offset in nfct_reasm skb */
113112
__u16 fragoffs; /* IPv6 fragment offset, 0 if first frag (or not frag)*/
114113
__s16 protocol;
115114
__s32 flags;
116115
union nf_inet_addr saddr;
117116
union nf_inet_addr daddr;
118117
};
119118

120-
/* Dependency to module: nf_defrag_ipv6 */
121-
#if defined(CONFIG_NF_DEFRAG_IPV6) || defined(CONFIG_NF_DEFRAG_IPV6_MODULE)
122-
static inline struct sk_buff *skb_nfct_reasm(const struct sk_buff *skb)
123-
{
124-
return skb->nfct_reasm;
125-
}
126-
static inline void *frag_safe_skb_hp(const struct sk_buff *skb, int offset,
127-
int len, void *buffer,
128-
const struct ip_vs_iphdr *ipvsh)
129-
{
130-
if (unlikely(ipvsh->fragoffs && skb_nfct_reasm(skb)))
131-
return skb_header_pointer(skb_nfct_reasm(skb),
132-
ipvsh->thoff_reasm, len, buffer);
133-
134-
return skb_header_pointer(skb, offset, len, buffer);
135-
}
136-
#else
137-
static inline struct sk_buff *skb_nfct_reasm(const struct sk_buff *skb)
138-
{
139-
return NULL;
140-
}
141119
static inline void *frag_safe_skb_hp(const struct sk_buff *skb, int offset,
142120
int len, void *buffer,
143121
const struct ip_vs_iphdr *ipvsh)
144122
{
145123
return skb_header_pointer(skb, offset, len, buffer);
146124
}
147-
#endif
148125

149126
static inline void
150127
ip_vs_fill_ip4hdr(const void *nh, struct ip_vs_iphdr *iphdr)
@@ -171,19 +148,12 @@ ip_vs_fill_iph_skb(int af, const struct sk_buff *skb, struct ip_vs_iphdr *iphdr)
171148
(struct ipv6hdr *)skb_network_header(skb);
172149
iphdr->saddr.in6 = iph->saddr;
173150
iphdr->daddr.in6 = iph->daddr;
174-
/* ipv6_find_hdr() updates len, flags, thoff_reasm */
175-
iphdr->thoff_reasm = 0;
151+
/* ipv6_find_hdr() updates len, flags */
176152
iphdr->len = 0;
177153
iphdr->flags = 0;
178154
iphdr->protocol = ipv6_find_hdr(skb, &iphdr->len, -1,
179155
&iphdr->fragoffs,
180156
&iphdr->flags);
181-
/* get proto from re-assembled packet and it's offset */
182-
if (skb_nfct_reasm(skb))
183-
iphdr->protocol = ipv6_find_hdr(skb_nfct_reasm(skb),
184-
&iphdr->thoff_reasm,
185-
-1, NULL, NULL);
186-
187157
} else
188158
#endif
189159
{

‎include/net/netfilter/ipv6/nf_defrag_ipv6.h‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ void nf_defrag_ipv6_enable(void);
66
int nf_ct_frag6_init(void);
77
void nf_ct_frag6_cleanup(void);
88
struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb, u32 user);
9-
void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb,
10-
struct net_device *in, struct net_device *out,
11-
int (*okfn)(struct sk_buff *));
9+
void nf_ct_frag6_consume_orig(struct sk_buff *skb);
1210

1311
struct inet_frags_ctl;
1412

‎net/core/skbuff.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,6 @@ static void skb_release_head_state(struct sk_buff *skb)
592592
#if IS_ENABLED(CONFIG_NF_CONNTRACK)
593593
nf_conntrack_put(skb->nfct);
594594
#endif
595-
#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
596-
nf_conntrack_put_reasm(skb->nfct_reasm);
597-
#endif
598595
#ifdef CONFIG_BRIDGE_NETFILTER
599596
nf_bridge_put(skb->nf_bridge);
600597
#endif

‎net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c‎

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -169,64 +169,13 @@ static unsigned int ipv6_confirm(const struct nf_hook_ops *ops,
169169
return nf_conntrack_confirm(skb);
170170
}
171171

172-
static unsigned int __ipv6_conntrack_in(struct net *net,
173-
unsigned int hooknum,
174-
struct sk_buff *skb,
175-
const struct net_device *in,
176-
const struct net_device *out,
177-
int (*okfn)(struct sk_buff *))
178-
{
179-
struct sk_buff *reasm = skb->nfct_reasm;
180-
const struct nf_conn_help *help;
181-
struct nf_conn *ct;
182-
enum ip_conntrack_info ctinfo;
183-
184-
/* This packet is fragmented and has reassembled packet. */
185-
if (reasm) {
186-
/* Reassembled packet isn't parsed yet ? */
187-
if (!reasm->nfct) {
188-
unsigned int ret;
189-
190-
ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm);
191-
if (ret != NF_ACCEPT)
192-
return ret;
193-
}
194-
195-
/* Conntrack helpers need the entire reassembled packet in the
196-
* POST_ROUTING hook. In case of unconfirmed connections NAT
197-
* might reassign a helper, so the entire packet is also
198-
* required.
199-
*/
200-
ct = nf_ct_get(reasm, &ctinfo);
201-
if (ct != NULL && !nf_ct_is_untracked(ct)) {
202-
help = nfct_help(ct);
203-
if ((help && help->helper) || !nf_ct_is_confirmed(ct)) {
204-
nf_conntrack_get_reasm(reasm);
205-
NF_HOOK_THRESH(NFPROTO_IPV6, hooknum, reasm,
206-
(struct net_device *)in,
207-
(struct net_device *)out,
208-
okfn, NF_IP6_PRI_CONNTRACK + 1);
209-
return NF_DROP_ERR(-ECANCELED);
210-
}
211-
}
212-
213-
nf_conntrack_get(reasm->nfct);
214-
skb->nfct = reasm->nfct;
215-
skb->nfctinfo = reasm->nfctinfo;
216-
return NF_ACCEPT;
217-
}
218-
219-
return nf_conntrack_in(net, PF_INET6, hooknum, skb);
220-
}
221-
222172
static unsigned int ipv6_conntrack_in(const struct nf_hook_ops *ops,
223173
struct sk_buff *skb,
224174
const struct net_device *in,
225175
const struct net_device *out,
226176
int (*okfn)(struct sk_buff *))
227177
{
228-
return __ipv6_conntrack_in(dev_net(in), ops->hooknum, skb, in, out,
229-
okfn);
178+
return nf_conntrack_in(dev_net(in), PF_INET6, ops->hooknum, skb);
230179
}
231180

232181
static unsigned int ipv6_conntrack_local(const struct nf_hook_ops *ops,
@@ -240,8 +189,7 @@ static unsigned int ipv6_conntrack_local(const struct nf_hook_ops *ops,
240189
net_notice_ratelimited("ipv6_conntrack_local: packet too short\n");
241190
return NF_ACCEPT;
242191
}
243-
return __ipv6_conntrack_in(dev_net(out), ops->hooknum, skb, in, out,
244-
okfn);
192+
return nf_conntrack_in(dev_net(out), PF_INET6, ops->hooknum, skb);
245193
}
246194

247195
static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {

‎net/ipv6/netfilter/nf_conntrack_reasm.c‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -633,31 +633,16 @@ struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb, u32 user)
633633
return skb;
634634
}
635635

636-
void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb,
637-
struct net_device *in, struct net_device *out,
638-
int (*okfn)(struct sk_buff *))
636+
void nf_ct_frag6_consume_orig(struct sk_buff *skb)
639637
{
640638
struct sk_buff *s, *s2;
641-
unsigned int ret = 0;
642639

643640
for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
644-
nf_conntrack_put_reasm(s->nfct_reasm);
645-
nf_conntrack_get_reasm(skb);
646-
s->nfct_reasm = skb;
647-
648641
s2 = s->next;
649642
s->next = NULL;
650-
651-
if (ret != -ECANCELED)
652-
ret = NF_HOOK_THRESH(NFPROTO_IPV6, hooknum, s,
653-
in, out, okfn,
654-
NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
655-
else
656-
kfree_skb(s);
657-
643+
consume_skb(s);
658644
s = s2;
659645
}
660-
nf_conntrack_put_reasm(skb);
661646
}
662647

663648
static int nf_ct_net_init(struct net *net)

‎net/ipv6/netfilter/nf_defrag_ipv6_hooks.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ static unsigned int ipv6_defrag(const struct nf_hook_ops *ops,
7575
if (reasm == skb)
7676
return NF_ACCEPT;
7777

78-
nf_ct_frag6_output(ops->hooknum, reasm, (struct net_device *)in,
79-
(struct net_device *)out, okfn);
78+
nf_ct_frag6_consume_orig(reasm);
79+
80+
NF_HOOK_THRESH(NFPROTO_IPV6, ops->hooknum, reasm,
81+
(struct net_device *) in, (struct net_device *) out,
82+
okfn, NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
8083

8184
return NF_STOLEN;
8285
}

‎net/netfilter/ipvs/ip_vs_core.c‎

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,12 +1139,6 @@ ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
11391139
ip_vs_fill_iph_skb(af, skb, &iph);
11401140
#ifdef CONFIG_IP_VS_IPV6
11411141
if (af == AF_INET6) {
1142-
if (!iph.fragoffs && skb_nfct_reasm(skb)) {
1143-
struct sk_buff *reasm = skb_nfct_reasm(skb);
1144-
/* Save fw mark for coming frags */
1145-
reasm->ipvs_property = 1;
1146-
reasm->mark = skb->mark;
1147-
}
11481142
if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
11491143
int related;
11501144
int verdict = ip_vs_out_icmp_v6(skb, &related,
@@ -1614,12 +1608,6 @@ ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
16141608

16151609
#ifdef CONFIG_IP_VS_IPV6
16161610
if (af == AF_INET6) {
1617-
if (!iph.fragoffs && skb_nfct_reasm(skb)) {
1618-
struct sk_buff *reasm = skb_nfct_reasm(skb);
1619-
/* Save fw mark for coming frags. */
1620-
reasm->ipvs_property = 1;
1621-
reasm->mark = skb->mark;
1622-
}
16231611
if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
16241612
int related;
16251613
int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum,
@@ -1671,9 +1659,8 @@ ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
16711659
/* sorry, all this trouble for a no-hit :) */
16721660
IP_VS_DBG_PKT(12, af, pp, skb, 0,
16731661
"ip_vs_in: packet continues traversal as normal");
1674-
if (iph.fragoffs && !skb_nfct_reasm(skb)) {
1662+
if (iph.fragoffs) {
16751663
/* Fragment that couldn't be mapped to a conn entry
1676-
* and don't have any pointer to a reasm skb
16771664
* is missing module nf_defrag_ipv6
16781665
*/
16791666
IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
@@ -1755,38 +1742,6 @@ ip_vs_local_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
17551742

17561743
#ifdef CONFIG_IP_VS_IPV6
17571744

1758-
/*
1759-
* AF_INET6 fragment handling
1760-
* Copy info from first fragment, to the rest of them.
1761-
*/
1762-
static unsigned int
1763-
ip_vs_preroute_frag6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1764-
const struct net_device *in,
1765-
const struct net_device *out,
1766-
int (*okfn)(struct sk_buff *))
1767-
{
1768-
struct sk_buff *reasm = skb_nfct_reasm(skb);
1769-
struct net *net;
1770-
1771-
/* Skip if not a "replay" from nf_ct_frag6_output or first fragment.
1772-
* ipvs_property is set when checking first fragment
1773-
* in ip_vs_in() and ip_vs_out().
1774-
*/
1775-
if (reasm)
1776-
IP_VS_DBG(2, "Fragment recv prop:%d\n", reasm->ipvs_property);
1777-
if (!reasm || !reasm->ipvs_property)
1778-
return NF_ACCEPT;
1779-
1780-
net = skb_net(skb);
1781-
if (!net_ipvs(net)->enable)
1782-
return NF_ACCEPT;
1783-
1784-
/* Copy stored fw mark, saved in ip_vs_{in,out} */
1785-
skb->mark = reasm->mark;
1786-
1787-
return NF_ACCEPT;
1788-
}
1789-
17901745
/*
17911746
* AF_INET6 handler in NF_INET_LOCAL_IN chain
17921747
* Schedule and forward packets from remote clients
@@ -1924,14 +1879,6 @@ static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
19241879
.priority = 100,
19251880
},
19261881
#ifdef CONFIG_IP_VS_IPV6
1927-
/* After mangle & nat fetch 2:nd fragment and following */
1928-
{
1929-
.hook = ip_vs_preroute_frag6,
1930-
.owner = THIS_MODULE,
1931-
.pf = NFPROTO_IPV6,
1932-
.hooknum = NF_INET_PRE_ROUTING,
1933-
.priority = NF_IP6_PRI_NAT_DST + 1,
1934-
},
19351882
/* After packet filtering, change source only for VS/NAT */
19361883
{
19371884
.hook = ip_vs_reply6,

0 commit comments

Comments
 (0)