blob: 9306ec4fab41e9fa0c3c99fd6be78bcf8adfb397 [file] [log] [blame]
KOVACS Krisztian73e40222008-10-08 11:35:12 +02001/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/types.h>
10#include <linux/ip.h>
11#include <linux/netfilter.h>
12#include <linux/module.h>
13#include <linux/skbuff.h>
14#include <net/route.h>
15#include <net/ip.h>
16
Patrick McHardy8fa9ff62009-12-15 16:59:59 +010017#include <linux/netfilter_bridge.h>
KOVACS Krisztian73e40222008-10-08 11:35:12 +020018#include <linux/netfilter_ipv4.h>
19#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
Duan Jiong24de3d32014-06-30 09:19:32 +080020#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Florian Westphal76780372010-02-08 15:39:16 +010021#include <net/netfilter/nf_conntrack.h>
Patrick McHardy37ee3d52010-02-18 19:04:44 +010022#endif
23#include <net/netfilter/nf_conntrack_zones.h>
KOVACS Krisztian73e40222008-10-08 11:35:12 +020024
KOVACS Krisztian73e40222008-10-08 11:35:12 +020025static int nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user)
26{
27 int err;
28
29 skb_orphan(skb);
30
31 local_bh_disable();
32 err = ip_defrag(skb, user);
33 local_bh_enable();
34
Florian Westphal895162b2014-05-02 15:32:16 +020035 if (!err) {
KOVACS Krisztian73e40222008-10-08 11:35:12 +020036 ip_send_check(ip_hdr(skb));
WANG Cong60ff7462014-05-04 16:39:18 -070037 skb->ignore_df = 1;
Florian Westphal895162b2014-05-02 15:32:16 +020038 }
KOVACS Krisztian73e40222008-10-08 11:35:12 +020039
40 return err;
41}
42
Patrick McHardy8fa9ff62009-12-15 16:59:59 +010043static enum ip_defrag_users nf_ct_defrag_user(unsigned int hooknum,
44 struct sk_buff *skb)
45{
Daniel Borkmann308ac912015-08-08 21:40:01 +020046 u16 zone_id = NF_CT_DEFAULT_ZONE_ID;
Duan Jiong24de3d32014-06-30 09:19:32 +080047#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Daniel Borkmanndeedb592015-08-14 16:03:39 +020048 if (skb->nfct) {
49 enum ip_conntrack_info ctinfo;
50 const struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
51
52 zone_id = nf_ct_zone_id(nf_ct_zone(ct), CTINFO2DIR(ctinfo));
53 }
Patrick McHardy37ee3d52010-02-18 19:04:44 +010054#endif
Florian Westphal72b1e5e2015-07-23 16:21:30 +020055 if (nf_bridge_in_prerouting(skb))
Daniel Borkmann308ac912015-08-08 21:40:01 +020056 return IP_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id;
Florian Westphal72b1e5e2015-07-23 16:21:30 +020057
Patrick McHardy8fa9ff62009-12-15 16:59:59 +010058 if (hooknum == NF_INET_PRE_ROUTING)
Daniel Borkmann308ac912015-08-08 21:40:01 +020059 return IP_DEFRAG_CONNTRACK_IN + zone_id;
Patrick McHardy8fa9ff62009-12-15 16:59:59 +010060 else
Daniel Borkmann308ac912015-08-08 21:40:01 +020061 return IP_DEFRAG_CONNTRACK_OUT + zone_id;
Patrick McHardy8fa9ff62009-12-15 16:59:59 +010062}
63
Patrick McHardy795aa6e2013-10-10 09:21:55 +020064static unsigned int ipv4_conntrack_defrag(const struct nf_hook_ops *ops,
KOVACS Krisztian73e40222008-10-08 11:35:12 +020065 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -040066 const struct nf_hook_state *state)
KOVACS Krisztian73e40222008-10-08 11:35:12 +020067{
Jiri Olsacbdd7692010-09-21 21:17:34 +000068 struct sock *sk = skb->sk;
Jiri Olsa7b2ff182010-06-15 01:07:31 +000069 struct inet_sock *inet = inet_sk(skb->sk);
70
Jiri Olsacbdd7692010-09-21 21:17:34 +000071 if (sk && (sk->sk_family == PF_INET) &&
72 inet->nodefrag)
Jiri Olsa7b2ff182010-06-15 01:07:31 +000073 return NF_ACCEPT;
74
Duan Jiong24de3d32014-06-30 09:19:32 +080075#if IS_ENABLED(CONFIG_NF_CONNTRACK)
76#if !IS_ENABLED(CONFIG_NF_NAT)
KOVACS Krisztian73e40222008-10-08 11:35:12 +020077 /* Previously seen (loopback)? Ignore. Do this before
78 fragment check. */
Patrick McHardyb2a15a62010-02-03 14:13:03 +010079 if (skb->nfct && !nf_ct_is_template((struct nf_conn *)skb->nfct))
KOVACS Krisztian73e40222008-10-08 11:35:12 +020080 return NF_ACCEPT;
81#endif
Patrick McHardy38f7ac32008-10-14 11:56:59 -070082#endif
KOVACS Krisztian73e40222008-10-08 11:35:12 +020083 /* Gather fragments. */
Paul Gortmaker56f8a752011-06-21 20:33:34 -070084 if (ip_is_fragment(ip_hdr(skb))) {
Patrick McHardy795aa6e2013-10-10 09:21:55 +020085 enum ip_defrag_users user =
86 nf_ct_defrag_user(ops->hooknum, skb);
87
Patrick McHardy8fa9ff62009-12-15 16:59:59 +010088 if (nf_ct_ipv4_gather_frags(skb, user))
KOVACS Krisztian73e40222008-10-08 11:35:12 +020089 return NF_STOLEN;
90 }
91 return NF_ACCEPT;
92}
93
94static struct nf_hook_ops ipv4_defrag_ops[] = {
95 {
96 .hook = ipv4_conntrack_defrag,
97 .owner = THIS_MODULE,
Alban Crequy89a48e32012-05-14 03:56:37 +000098 .pf = NFPROTO_IPV4,
KOVACS Krisztian73e40222008-10-08 11:35:12 +020099 .hooknum = NF_INET_PRE_ROUTING,
100 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
101 },
102 {
103 .hook = ipv4_conntrack_defrag,
104 .owner = THIS_MODULE,
Alban Crequy89a48e32012-05-14 03:56:37 +0000105 .pf = NFPROTO_IPV4,
KOVACS Krisztian73e40222008-10-08 11:35:12 +0200106 .hooknum = NF_INET_LOCAL_OUT,
107 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
108 },
109};
110
111static int __init nf_defrag_init(void)
112{
113 return nf_register_hooks(ipv4_defrag_ops, ARRAY_SIZE(ipv4_defrag_ops));
114}
115
116static void __exit nf_defrag_fini(void)
117{
118 nf_unregister_hooks(ipv4_defrag_ops, ARRAY_SIZE(ipv4_defrag_ops));
119}
120
121void nf_defrag_ipv4_enable(void)
122{
123}
124EXPORT_SYMBOL_GPL(nf_defrag_ipv4_enable);
125
126module_init(nf_defrag_init);
127module_exit(nf_defrag_fini);
128
129MODULE_LICENSE("GPL");