Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netfilter: nf_defrag: Skip defrag if NOTRACK is set

conntrack defrag is needed only if some module like CONNTRACK or NAT
explicitly requests it. For plain forwarding scenarios, defrag is
not needed and can be skipped if NOTRACK is set in a rule.

Since conntrack defrag is currently higher priority than raw table,
setting NOTRACK is not sufficient. We need to move raw to a higher
priority for iptables only.

This is achieved by introducing a module parameter "raw_before_defrag"
which allows to change the priority of raw table to place it before
defrag. By default, the parameter is disabled and the priority of raw
table is NF_IP_PRI_RAW to support legacy behavior. If the module
parameter is enabled, then the priority of the raw table is set to
NF_IP_PRI_RAW_BEFORE_DEFRAG.

Signed-off-by: Subash Abhinov Kasiviswanathan <subashab@codeaurora.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>

authored by

Subash Abhinov Kasiviswanathan and committed by
Pablo Neira Ayuso
902d6a4c 5ed001ba

+30 -3
+1
include/uapi/linux/netfilter_ipv4.h
··· 57 57 58 58 enum nf_ip_hook_priorities { 59 59 NF_IP_PRI_FIRST = INT_MIN, 60 + NF_IP_PRI_RAW_BEFORE_DEFRAG = -450, 60 61 NF_IP_PRI_CONNTRACK_DEFRAG = -400, 61 62 NF_IP_PRI_RAW = -300, 62 63 NF_IP_PRI_SELINUX_FIRST = -225,
+1
include/uapi/linux/netfilter_ipv6.h
··· 62 62 63 63 enum nf_ip6_hook_priorities { 64 64 NF_IP6_PRI_FIRST = INT_MIN, 65 + NF_IP6_PRI_RAW_BEFORE_DEFRAG = -450, 65 66 NF_IP6_PRI_CONNTRACK_DEFRAG = -400, 66 67 NF_IP6_PRI_RAW = -300, 67 68 NF_IP6_PRI_SELINUX_FIRST = -225,
+12 -1
net/ipv4/netfilter/iptable_raw.c
··· 3 3 * 4 4 * Copyright (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> 5 5 */ 6 + #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 6 7 #include <linux/module.h> 7 8 #include <linux/netfilter_ipv4/ip_tables.h> 8 9 #include <linux/slab.h> ··· 13 12 14 13 static int __net_init iptable_raw_table_init(struct net *net); 15 14 16 - static const struct xt_table packet_raw = { 15 + static bool raw_before_defrag __read_mostly; 16 + MODULE_PARM_DESC(raw_before_defrag, "Enable raw table before defrag"); 17 + module_param(raw_before_defrag, bool, 0000); 18 + 19 + static struct xt_table packet_raw = { 17 20 .name = "raw", 18 21 .valid_hooks = RAW_VALID_HOOKS, 19 22 .me = THIS_MODULE, ··· 68 63 static int __init iptable_raw_init(void) 69 64 { 70 65 int ret; 66 + 67 + if (raw_before_defrag) { 68 + packet_raw.priority = NF_IP_PRI_RAW_BEFORE_DEFRAG; 69 + 70 + pr_info("Enabling raw table before defrag\n"); 71 + } 71 72 72 73 rawtable_ops = xt_hook_ops_alloc(&packet_raw, iptable_raw_hook); 73 74 if (IS_ERR(rawtable_ops))
+1 -1
net/ipv4/netfilter/nf_defrag_ipv4.c
··· 80 80 #endif 81 81 #endif 82 82 /* Gather fragments. */ 83 - if (ip_is_fragment(ip_hdr(skb))) { 83 + if (skb->_nfct != IP_CT_UNTRACKED && ip_is_fragment(ip_hdr(skb))) { 84 84 enum ip_defrag_users user = 85 85 nf_ct_defrag_user(state->hook, skb); 86 86
+12 -1
net/ipv6/netfilter/ip6table_raw.c
··· 3 3 * 4 4 * Copyright (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> 5 5 */ 6 + #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 6 7 #include <linux/module.h> 7 8 #include <linux/netfilter_ipv6/ip6_tables.h> 8 9 #include <linux/slab.h> ··· 12 11 13 12 static int __net_init ip6table_raw_table_init(struct net *net); 14 13 15 - static const struct xt_table packet_raw = { 14 + static bool raw_before_defrag __read_mostly; 15 + MODULE_PARM_DESC(raw_before_defrag, "Enable raw table before defrag"); 16 + module_param(raw_before_defrag, bool, 0000); 17 + 18 + static struct xt_table packet_raw = { 16 19 .name = "raw", 17 20 .valid_hooks = RAW_VALID_HOOKS, 18 21 .me = THIS_MODULE, ··· 67 62 static int __init ip6table_raw_init(void) 68 63 { 69 64 int ret; 65 + 66 + if (raw_before_defrag) { 67 + packet_raw.priority = NF_IP6_PRI_RAW_BEFORE_DEFRAG; 68 + 69 + pr_info("Enabling raw table before defrag\n"); 70 + } 70 71 71 72 /* Register hooks */ 72 73 rawtable_ops = xt_hook_ops_alloc(&packet_raw, ip6table_raw_hook);
+3
net/ipv6/netfilter/nf_defrag_ipv6_hooks.c
··· 65 65 return NF_ACCEPT; 66 66 #endif 67 67 68 + if (skb->_nfct == IP_CT_UNTRACKED) 69 + return NF_ACCEPT; 70 + 68 71 err = nf_ct_frag6_gather(state->net, skb, 69 72 nf_ct6_defrag_user(state->hook, skb)); 70 73 /* queued */