core, nfqueue, openvswitch: Orphan frags in skb_zerocopy and handle errors
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / nf_conntrack_proto_generic.c
CommitLineData
9fb9cbb1
YK
1/* (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.
9fb9cbb1
YK
7 */
8
9#include <linux/types.h>
cd354f1a 10#include <linux/jiffies.h>
9fb9cbb1
YK
11#include <linux/timer.h>
12#include <linux/netfilter.h>
605dcad6 13#include <net/netfilter/nf_conntrack_l4proto.h>
9fb9cbb1 14
933a41e7 15static unsigned int nf_ct_generic_timeout __read_mostly = 600*HZ;
9fb9cbb1 16
15f585bd
G
17static inline struct nf_generic_net *generic_pernet(struct net *net)
18{
19 return &net->ct.nf_ct_proto.generic;
20}
21
09f263cd
JE
22static bool generic_pkt_to_tuple(const struct sk_buff *skb,
23 unsigned int dataoff,
24 struct nf_conntrack_tuple *tuple)
9fb9cbb1
YK
25{
26 tuple->src.u.all = 0;
27 tuple->dst.u.all = 0;
28
09f263cd 29 return true;
9fb9cbb1
YK
30}
31
09f263cd
JE
32static bool generic_invert_tuple(struct nf_conntrack_tuple *tuple,
33 const struct nf_conntrack_tuple *orig)
9fb9cbb1
YK
34{
35 tuple->src.u.all = 0;
36 tuple->dst.u.all = 0;
37
09f263cd 38 return true;
9fb9cbb1
YK
39}
40
41/* Print out the per-protocol part of the tuple. */
42static int generic_print_tuple(struct seq_file *s,
43 const struct nf_conntrack_tuple *tuple)
44{
45 return 0;
46}
47
2c8503f5
PNA
48static unsigned int *generic_get_timeouts(struct net *net)
49{
15f585bd 50 return &(generic_pernet(net)->timeout);
2c8503f5
PNA
51}
52
9fb9cbb1 53/* Returns verdict for packet, or -1 for invalid. */
2c8503f5
PNA
54static int generic_packet(struct nf_conn *ct,
55 const struct sk_buff *skb,
56 unsigned int dataoff,
57 enum ip_conntrack_info ctinfo,
58 u_int8_t pf,
59 unsigned int hooknum,
60 unsigned int *timeout)
9fb9cbb1 61{
2c8503f5 62 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
9fb9cbb1
YK
63 return NF_ACCEPT;
64}
65
66/* Called when a new connection for this protocol found. */
2c8503f5
PNA
67static bool generic_new(struct nf_conn *ct, const struct sk_buff *skb,
68 unsigned int dataoff, unsigned int *timeouts)
9fb9cbb1 69{
09f263cd 70 return true;
9fb9cbb1
YK
71}
72
50978462
PNA
73#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
74
75#include <linux/netfilter/nfnetlink.h>
76#include <linux/netfilter/nfnetlink_cttimeout.h>
77
8264deb8
G
78static int generic_timeout_nlattr_to_obj(struct nlattr *tb[],
79 struct net *net, void *data)
50978462
PNA
80{
81 unsigned int *timeout = data;
8264deb8 82 struct nf_generic_net *gn = generic_pernet(net);
50978462
PNA
83
84 if (tb[CTA_TIMEOUT_GENERIC_TIMEOUT])
85 *timeout =
86 ntohl(nla_get_be32(tb[CTA_TIMEOUT_GENERIC_TIMEOUT])) * HZ;
87 else {
88 /* Set default generic timeout. */
8264deb8 89 *timeout = gn->timeout;
50978462
PNA
90 }
91
92 return 0;
93}
94
95static int
96generic_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
97{
98 const unsigned int *timeout = data;
99
f5776941
DM
100 if (nla_put_be32(skb, CTA_TIMEOUT_GENERIC_TIMEOUT, htonl(*timeout / HZ)))
101 goto nla_put_failure;
50978462
PNA
102
103 return 0;
104
105nla_put_failure:
106 return -ENOSPC;
107}
108
109static const struct nla_policy
110generic_timeout_nla_policy[CTA_TIMEOUT_GENERIC_MAX+1] = {
111 [CTA_TIMEOUT_GENERIC_TIMEOUT] = { .type = NLA_U32 },
112};
113#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
114
933a41e7 115#ifdef CONFIG_SYSCTL
933a41e7
PM
116static struct ctl_table generic_sysctl_table[] = {
117 {
933a41e7 118 .procname = "nf_conntrack_generic_timeout",
933a41e7
PM
119 .maxlen = sizeof(unsigned int),
120 .mode = 0644,
6d9f239a 121 .proc_handler = proc_dointvec_jiffies,
933a41e7 122 },
f8572d8f 123 { }
933a41e7 124};
a999e683
PM
125#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
126static struct ctl_table generic_compat_sysctl_table[] = {
127 {
a999e683 128 .procname = "ip_conntrack_generic_timeout",
a999e683
PM
129 .maxlen = sizeof(unsigned int),
130 .mode = 0644,
6d9f239a 131 .proc_handler = proc_dointvec_jiffies,
a999e683 132 },
f8572d8f 133 { }
a999e683
PM
134};
135#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
933a41e7
PM
136#endif /* CONFIG_SYSCTL */
137
22ac0377
G
138static int generic_kmemdup_sysctl_table(struct nf_proto_net *pn,
139 struct nf_generic_net *gn)
15f585bd 140{
15f585bd
G
141#ifdef CONFIG_SYSCTL
142 pn->ctl_table = kmemdup(generic_sysctl_table,
143 sizeof(generic_sysctl_table),
144 GFP_KERNEL);
145 if (!pn->ctl_table)
146 return -ENOMEM;
22ac0377 147
15f585bd 148 pn->ctl_table[0].data = &gn->timeout;
22ac0377
G
149#endif
150 return 0;
151}
15f585bd 152
22ac0377
G
153static int generic_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
154 struct nf_generic_net *gn)
155{
156#ifdef CONFIG_SYSCTL
15f585bd
G
157#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
158 pn->ctl_compat_table = kmemdup(generic_compat_sysctl_table,
159 sizeof(generic_compat_sysctl_table),
160 GFP_KERNEL);
22ac0377 161 if (!pn->ctl_compat_table)
15f585bd 162 return -ENOMEM;
22ac0377 163
15f585bd
G
164 pn->ctl_compat_table[0].data = &gn->timeout;
165#endif
166#endif
167 return 0;
168}
169
22ac0377
G
170static int generic_init_net(struct net *net, u_int16_t proto)
171{
172 int ret;
173 struct nf_generic_net *gn = generic_pernet(net);
174 struct nf_proto_net *pn = &gn->pn;
175
176 gn->timeout = nf_ct_generic_timeout;
177
178 ret = generic_kmemdup_compat_sysctl_table(pn, gn);
179 if (ret < 0)
180 return ret;
181
182 ret = generic_kmemdup_sysctl_table(pn, gn);
183 if (ret < 0)
184 nf_ct_kfree_compat_sysctl_table(pn);
185
186 return ret;
187}
188
08911475
PNA
189static struct nf_proto_net *generic_get_net_proto(struct net *net)
190{
191 return &net->ct.nf_ct_proto.generic.pn;
192}
193
61075af5 194struct nf_conntrack_l4proto nf_conntrack_l4proto_generic __read_mostly =
9fb9cbb1
YK
195{
196 .l3proto = PF_UNSPEC,
fe2a7ce4 197 .l4proto = 255,
9fb9cbb1
YK
198 .name = "unknown",
199 .pkt_to_tuple = generic_pkt_to_tuple,
200 .invert_tuple = generic_invert_tuple,
201 .print_tuple = generic_print_tuple,
2c8503f5
PNA
202 .packet = generic_packet,
203 .get_timeouts = generic_get_timeouts,
204 .new = generic_new,
50978462
PNA
205#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
206 .ctnl_timeout = {
207 .nlattr_to_obj = generic_timeout_nlattr_to_obj,
208 .obj_to_nlattr = generic_timeout_obj_to_nlattr,
209 .nlattr_max = CTA_TIMEOUT_GENERIC_MAX,
210 .obj_size = sizeof(unsigned int),
211 .nla_policy = generic_timeout_nla_policy,
212 },
213#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
15f585bd 214 .init_net = generic_init_net,
08911475 215 .get_net_proto = generic_get_net_proto,
9fb9cbb1 216};