blob: d88da36b383ccb4fa5b431b6c5cb83b218cb68ad [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
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -050025static int nf_ct_ipv4_gather_frags(struct net *net, struct sk_buff *skb,
26 u_int32_t user)
KOVACS Krisztian73e40222008-10-08 11:35:12 +020027{
28 int err;
29
KOVACS Krisztian73e40222008-10-08 11:35:12 +020030 local_bh_disable();
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -050031 err = ip_defrag(net, skb, user);
KOVACS Krisztian73e40222008-10-08 11:35:12 +020032 local_bh_enable();
33
Joe Stringer5f547392016-02-03 10:00:10 -080034 if (!err)
WANG Cong60ff7462014-05-04 16:39:18 -070035 skb->ignore_df = 1;
KOVACS Krisztian73e40222008-10-08 11:35:12 +020036
37 return err;
38}
39
Patrick McHardy8fa9ff62009-12-15 16:59:59 +010040static enum ip_defrag_users nf_ct_defrag_user(unsigned int hooknum,
41 struct sk_buff *skb)
42{
Daniel Borkmann308ac912015-08-08 21:40:01 +020043 u16 zone_id = NF_CT_DEFAULT_ZONE_ID;
Duan Jiong24de3d32014-06-30 09:19:32 +080044#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Daniel Borkmanndeedb592015-08-14 16:03:39 +020045 if (skb->nfct) {
46 enum ip_conntrack_info ctinfo;
47 const struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
48
49 zone_id = nf_ct_zone_id(nf_ct_zone(ct), CTINFO2DIR(ctinfo));
50 }
Patrick McHardy37ee3d52010-02-18 19:04:44 +010051#endif
Florian Westphal72b1e5e2015-07-23 16:21:30 +020052 if (nf_bridge_in_prerouting(skb))
Daniel Borkmann308ac912015-08-08 21:40:01 +020053 return IP_DEFRAG_CONNTRACK_BRIDGE_IN + zone_id;
Florian Westphal72b1e5e2015-07-23 16:21:30 +020054
Patrick McHardy8fa9ff62009-12-15 16:59:59 +010055 if (hooknum == NF_INET_PRE_ROUTING)
Daniel Borkmann308ac912015-08-08 21:40:01 +020056 return IP_DEFRAG_CONNTRACK_IN + zone_id;
Patrick McHardy8fa9ff62009-12-15 16:59:59 +010057 else
Daniel Borkmann308ac912015-08-08 21:40:01 +020058 return IP_DEFRAG_CONNTRACK_OUT + zone_id;
Patrick McHardy8fa9ff62009-12-15 16:59:59 +010059}
60
Eric W. Biederman06198b32015-09-18 14:33:06 -050061static unsigned int ipv4_conntrack_defrag(void *priv,
KOVACS Krisztian73e40222008-10-08 11:35:12 +020062 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -040063 const struct nf_hook_state *state)
KOVACS Krisztian73e40222008-10-08 11:35:12 +020064{
Jiri Olsacbdd7692010-09-21 21:17:34 +000065 struct sock *sk = skb->sk;
Jiri Olsa7b2ff182010-06-15 01:07:31 +000066
Eric Dumazetf668f5f2015-11-05 11:34:06 -080067 if (sk && sk_fullsock(sk) && (sk->sk_family == PF_INET) &&
68 inet_sk(sk)->nodefrag)
Jiri Olsa7b2ff182010-06-15 01:07:31 +000069 return NF_ACCEPT;
70
Duan Jiong24de3d32014-06-30 09:19:32 +080071#if IS_ENABLED(CONFIG_NF_CONNTRACK)
72#if !IS_ENABLED(CONFIG_NF_NAT)
KOVACS Krisztian73e40222008-10-08 11:35:12 +020073 /* Previously seen (loopback)? Ignore. Do this before
74 fragment check. */
Patrick McHardyb2a15a62010-02-03 14:13:03 +010075 if (skb->nfct && !nf_ct_is_template((struct nf_conn *)skb->nfct))
KOVACS Krisztian73e40222008-10-08 11:35:12 +020076 return NF_ACCEPT;
77#endif
Patrick McHardy38f7ac32008-10-14 11:56:59 -070078#endif
KOVACS Krisztian73e40222008-10-08 11:35:12 +020079 /* Gather fragments. */
Paul Gortmaker56f8a752011-06-21 20:33:34 -070080 if (ip_is_fragment(ip_hdr(skb))) {
Patrick McHardy795aa6e2013-10-10 09:21:55 +020081 enum ip_defrag_users user =
Eric W. Biederman082a7582015-09-18 14:32:56 -050082 nf_ct_defrag_user(state->hook, skb);
Patrick McHardy795aa6e2013-10-10 09:21:55 +020083
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -050084 if (nf_ct_ipv4_gather_frags(state->net, skb, user))
KOVACS Krisztian73e40222008-10-08 11:35:12 +020085 return NF_STOLEN;
86 }
87 return NF_ACCEPT;
88}
89
90static struct nf_hook_ops ipv4_defrag_ops[] = {
91 {
92 .hook = ipv4_conntrack_defrag,
Alban Crequy89a48e32012-05-14 03:56:37 +000093 .pf = NFPROTO_IPV4,
KOVACS Krisztian73e40222008-10-08 11:35:12 +020094 .hooknum = NF_INET_PRE_ROUTING,
95 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
96 },
97 {
98 .hook = ipv4_conntrack_defrag,
Alban Crequy89a48e32012-05-14 03:56:37 +000099 .pf = NFPROTO_IPV4,
KOVACS Krisztian73e40222008-10-08 11:35:12 +0200100 .hooknum = NF_INET_LOCAL_OUT,
101 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
102 },
103};
104
105static int __init nf_defrag_init(void)
106{
107 return nf_register_hooks(ipv4_defrag_ops, ARRAY_SIZE(ipv4_defrag_ops));
108}
109
110static void __exit nf_defrag_fini(void)
111{
112 nf_unregister_hooks(ipv4_defrag_ops, ARRAY_SIZE(ipv4_defrag_ops));
113}
114
115void nf_defrag_ipv4_enable(void)
116{
117}
118EXPORT_SYMBOL_GPL(nf_defrag_ipv4_enable);
119
120module_init(nf_defrag_init);
121module_exit(nf_defrag_fini);
122
123MODULE_LICENSE("GPL");