GNU Linux-libre 4.14.254-gnu1
[releases.git] / net / netfilter / nf_conntrack_proto_generic.c
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.
7  */
8
9 #include <linux/types.h>
10 #include <linux/jiffies.h>
11 #include <linux/timer.h>
12 #include <linux/netfilter.h>
13 #include <net/netfilter/nf_conntrack_l4proto.h>
14
15 static unsigned int nf_ct_generic_timeout __read_mostly = 600*HZ;
16
17 static bool nf_generic_should_process(u8 proto)
18 {
19         switch (proto) {
20 #ifdef CONFIG_NF_CT_PROTO_GRE_MODULE
21         case IPPROTO_GRE:
22                 return false;
23 #endif
24         default:
25                 return true;
26         }
27 }
28
29 static inline struct nf_generic_net *generic_pernet(struct net *net)
30 {
31         return &net->ct.nf_ct_proto.generic;
32 }
33
34 static bool generic_pkt_to_tuple(const struct sk_buff *skb,
35                                  unsigned int dataoff,
36                                  struct net *net, struct nf_conntrack_tuple *tuple)
37 {
38         tuple->src.u.all = 0;
39         tuple->dst.u.all = 0;
40
41         return true;
42 }
43
44 static bool generic_invert_tuple(struct nf_conntrack_tuple *tuple,
45                                  const struct nf_conntrack_tuple *orig)
46 {
47         tuple->src.u.all = 0;
48         tuple->dst.u.all = 0;
49
50         return true;
51 }
52
53 static unsigned int *generic_get_timeouts(struct net *net)
54 {
55         return &(generic_pernet(net)->timeout);
56 }
57
58 /* Returns verdict for packet, or -1 for invalid. */
59 static int generic_packet(struct nf_conn *ct,
60                           const struct sk_buff *skb,
61                           unsigned int dataoff,
62                           enum ip_conntrack_info ctinfo,
63                           u_int8_t pf,
64                           unsigned int *timeout)
65 {
66         nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
67         return NF_ACCEPT;
68 }
69
70 /* Called when a new connection for this protocol found. */
71 static bool generic_new(struct nf_conn *ct, const struct sk_buff *skb,
72                         unsigned int dataoff, unsigned int *timeouts)
73 {
74         bool ret;
75
76         ret = nf_generic_should_process(nf_ct_protonum(ct));
77         if (!ret)
78                 pr_warn_once("conntrack: generic helper won't handle protocol %d. Please consider loading the specific helper module.\n",
79                              nf_ct_protonum(ct));
80         return ret;
81 }
82
83 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
84
85 #include <linux/netfilter/nfnetlink.h>
86 #include <linux/netfilter/nfnetlink_cttimeout.h>
87
88 static int generic_timeout_nlattr_to_obj(struct nlattr *tb[],
89                                          struct net *net, void *data)
90 {
91         unsigned int *timeout = data;
92         struct nf_generic_net *gn = generic_pernet(net);
93
94         if (tb[CTA_TIMEOUT_GENERIC_TIMEOUT])
95                 *timeout =
96                     ntohl(nla_get_be32(tb[CTA_TIMEOUT_GENERIC_TIMEOUT])) * HZ;
97         else {
98                 /* Set default generic timeout. */
99                 *timeout = gn->timeout;
100         }
101
102         return 0;
103 }
104
105 static int
106 generic_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
107 {
108         const unsigned int *timeout = data;
109
110         if (nla_put_be32(skb, CTA_TIMEOUT_GENERIC_TIMEOUT, htonl(*timeout / HZ)))
111                 goto nla_put_failure;
112
113         return 0;
114
115 nla_put_failure:
116         return -ENOSPC;
117 }
118
119 static const struct nla_policy
120 generic_timeout_nla_policy[CTA_TIMEOUT_GENERIC_MAX+1] = {
121         [CTA_TIMEOUT_GENERIC_TIMEOUT]   = { .type = NLA_U32 },
122 };
123 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
124
125 #ifdef CONFIG_SYSCTL
126 static struct ctl_table generic_sysctl_table[] = {
127         {
128                 .procname       = "nf_conntrack_generic_timeout",
129                 .maxlen         = sizeof(unsigned int),
130                 .mode           = 0644,
131                 .proc_handler   = proc_dointvec_jiffies,
132         },
133         { }
134 };
135 #endif /* CONFIG_SYSCTL */
136
137 static int generic_kmemdup_sysctl_table(struct nf_proto_net *pn,
138                                         struct nf_generic_net *gn)
139 {
140 #ifdef CONFIG_SYSCTL
141         pn->ctl_table = kmemdup(generic_sysctl_table,
142                                 sizeof(generic_sysctl_table),
143                                 GFP_KERNEL);
144         if (!pn->ctl_table)
145                 return -ENOMEM;
146
147         pn->ctl_table[0].data = &gn->timeout;
148 #endif
149         return 0;
150 }
151
152 static int generic_init_net(struct net *net, u_int16_t proto)
153 {
154         struct nf_generic_net *gn = generic_pernet(net);
155         struct nf_proto_net *pn = &gn->pn;
156
157         gn->timeout = nf_ct_generic_timeout;
158
159         return generic_kmemdup_sysctl_table(pn, gn);
160 }
161
162 static struct nf_proto_net *generic_get_net_proto(struct net *net)
163 {
164         return &net->ct.nf_ct_proto.generic.pn;
165 }
166
167 struct nf_conntrack_l4proto nf_conntrack_l4proto_generic __read_mostly =
168 {
169         .l3proto                = PF_UNSPEC,
170         .l4proto                = 255,
171         .pkt_to_tuple           = generic_pkt_to_tuple,
172         .invert_tuple           = generic_invert_tuple,
173         .packet                 = generic_packet,
174         .get_timeouts           = generic_get_timeouts,
175         .new                    = generic_new,
176 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
177         .ctnl_timeout           = {
178                 .nlattr_to_obj  = generic_timeout_nlattr_to_obj,
179                 .obj_to_nlattr  = generic_timeout_obj_to_nlattr,
180                 .nlattr_max     = CTA_TIMEOUT_GENERIC_MAX,
181                 .obj_size       = sizeof(unsigned int),
182                 .nla_policy     = generic_timeout_nla_policy,
183         },
184 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
185         .init_net               = generic_init_net,
186         .get_net_proto          = generic_get_net_proto,
187 };