]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/netfilter/nf_conntrack_proto_udplite.c
Merge branch 'i2c/for-current' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[karo-tx-linux.git] / net / netfilter / nf_conntrack_proto_udplite.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  * (C) 2007 Patrick McHardy <kaber@trash.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/types.h>
11 #include <linux/timer.h>
12 #include <linux/udp.h>
13 #include <linux/seq_file.h>
14 #include <linux/skbuff.h>
15 #include <linux/ipv6.h>
16 #include <net/ip6_checksum.h>
17 #include <net/checksum.h>
18
19 #include <linux/netfilter.h>
20 #include <linux/netfilter_ipv4.h>
21 #include <linux/netfilter_ipv6.h>
22 #include <net/netfilter/nf_conntrack_l4proto.h>
23 #include <net/netfilter/nf_conntrack_ecache.h>
24 #include <net/netfilter/nf_log.h>
25
26 static unsigned int udplite_timeouts[UDPLITE_CT_MAX] = {
27         [UDPLITE_CT_UNREPLIED]  = 30*HZ,
28         [UDPLITE_CT_REPLIED]    = 180*HZ,
29 };
30
31 static inline struct nf_udplite_net *udplite_pernet(struct net *net)
32 {
33         return &net->ct.nf_ct_proto.udplite;
34 }
35
36 static bool udplite_pkt_to_tuple(const struct sk_buff *skb,
37                                  unsigned int dataoff,
38                                  struct net *net,
39                                  struct nf_conntrack_tuple *tuple)
40 {
41         const struct udphdr *hp;
42         struct udphdr _hdr;
43
44         /* Actually only need first 4 bytes to get ports. */
45         hp = skb_header_pointer(skb, dataoff, 4, &_hdr);
46         if (hp == NULL)
47                 return false;
48
49         tuple->src.u.udp.port = hp->source;
50         tuple->dst.u.udp.port = hp->dest;
51         return true;
52 }
53
54 static bool udplite_invert_tuple(struct nf_conntrack_tuple *tuple,
55                                  const struct nf_conntrack_tuple *orig)
56 {
57         tuple->src.u.udp.port = orig->dst.u.udp.port;
58         tuple->dst.u.udp.port = orig->src.u.udp.port;
59         return true;
60 }
61
62 /* Print out the per-protocol part of the tuple. */
63 static void udplite_print_tuple(struct seq_file *s,
64                                 const struct nf_conntrack_tuple *tuple)
65 {
66         seq_printf(s, "sport=%hu dport=%hu ",
67                    ntohs(tuple->src.u.udp.port),
68                    ntohs(tuple->dst.u.udp.port));
69 }
70
71 static unsigned int *udplite_get_timeouts(struct net *net)
72 {
73         return udplite_pernet(net)->timeouts;
74 }
75
76 /* Returns verdict for packet, and may modify conntracktype */
77 static int udplite_packet(struct nf_conn *ct,
78                           const struct sk_buff *skb,
79                           unsigned int dataoff,
80                           enum ip_conntrack_info ctinfo,
81                           u_int8_t pf,
82                           unsigned int hooknum,
83                           unsigned int *timeouts)
84 {
85         /* If we've seen traffic both ways, this is some kind of UDP
86            stream.  Extend timeout. */
87         if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
88                 nf_ct_refresh_acct(ct, ctinfo, skb,
89                                    timeouts[UDPLITE_CT_REPLIED]);
90                 /* Also, more likely to be important, and not a probe */
91                 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
92                         nf_conntrack_event_cache(IPCT_ASSURED, ct);
93         } else {
94                 nf_ct_refresh_acct(ct, ctinfo, skb,
95                                    timeouts[UDPLITE_CT_UNREPLIED]);
96         }
97         return NF_ACCEPT;
98 }
99
100 /* Called when a new connection for this protocol found. */
101 static bool udplite_new(struct nf_conn *ct, const struct sk_buff *skb,
102                         unsigned int dataoff, unsigned int *timeouts)
103 {
104         return true;
105 }
106
107 static int udplite_error(struct net *net, struct nf_conn *tmpl,
108                          struct sk_buff *skb,
109                          unsigned int dataoff,
110                          enum ip_conntrack_info *ctinfo,
111                          u_int8_t pf,
112                          unsigned int hooknum)
113 {
114         unsigned int udplen = skb->len - dataoff;
115         const struct udphdr *hdr;
116         struct udphdr _hdr;
117         unsigned int cscov;
118
119         /* Header is too small? */
120         hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
121         if (hdr == NULL) {
122                 if (LOG_INVALID(net, IPPROTO_UDPLITE))
123                         nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
124                                       "nf_ct_udplite: short packet ");
125                 return -NF_ACCEPT;
126         }
127
128         cscov = ntohs(hdr->len);
129         if (cscov == 0)
130                 cscov = udplen;
131         else if (cscov < sizeof(*hdr) || cscov > udplen) {
132                 if (LOG_INVALID(net, IPPROTO_UDPLITE))
133                         nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
134                                 "nf_ct_udplite: invalid checksum coverage ");
135                 return -NF_ACCEPT;
136         }
137
138         /* UDPLITE mandates checksums */
139         if (!hdr->check) {
140                 if (LOG_INVALID(net, IPPROTO_UDPLITE))
141                         nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
142                                       "nf_ct_udplite: checksum missing ");
143                 return -NF_ACCEPT;
144         }
145
146         /* Checksum invalid? Ignore. */
147         if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
148             nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_UDP,
149                                 pf)) {
150                 if (LOG_INVALID(net, IPPROTO_UDPLITE))
151                         nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
152                                       "nf_ct_udplite: bad UDPLite checksum ");
153                 return -NF_ACCEPT;
154         }
155
156         return NF_ACCEPT;
157 }
158
159 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
160
161 #include <linux/netfilter/nfnetlink.h>
162 #include <linux/netfilter/nfnetlink_cttimeout.h>
163
164 static int udplite_timeout_nlattr_to_obj(struct nlattr *tb[],
165                                          struct net *net, void *data)
166 {
167         unsigned int *timeouts = data;
168         struct nf_udplite_net *un = udplite_pernet(net);
169
170         /* set default timeouts for UDPlite. */
171         timeouts[UDPLITE_CT_UNREPLIED] = un->timeouts[UDPLITE_CT_UNREPLIED];
172         timeouts[UDPLITE_CT_REPLIED] = un->timeouts[UDPLITE_CT_REPLIED];
173
174         if (tb[CTA_TIMEOUT_UDPLITE_UNREPLIED]) {
175                 timeouts[UDPLITE_CT_UNREPLIED] =
176                   ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_UNREPLIED])) * HZ;
177         }
178         if (tb[CTA_TIMEOUT_UDPLITE_REPLIED]) {
179                 timeouts[UDPLITE_CT_REPLIED] =
180                   ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_REPLIED])) * HZ;
181         }
182         return 0;
183 }
184
185 static int
186 udplite_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
187 {
188         const unsigned int *timeouts = data;
189
190         if (nla_put_be32(skb, CTA_TIMEOUT_UDPLITE_UNREPLIED,
191                          htonl(timeouts[UDPLITE_CT_UNREPLIED] / HZ)) ||
192             nla_put_be32(skb, CTA_TIMEOUT_UDPLITE_REPLIED,
193                          htonl(timeouts[UDPLITE_CT_REPLIED] / HZ)))
194                 goto nla_put_failure;
195         return 0;
196
197 nla_put_failure:
198         return -ENOSPC;
199 }
200
201 static const struct nla_policy
202 udplite_timeout_nla_policy[CTA_TIMEOUT_UDPLITE_MAX+1] = {
203         [CTA_TIMEOUT_UDPLITE_UNREPLIED] = { .type = NLA_U32 },
204         [CTA_TIMEOUT_UDPLITE_REPLIED]   = { .type = NLA_U32 },
205 };
206 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
207
208 #ifdef CONFIG_SYSCTL
209 static struct ctl_table udplite_sysctl_table[] = {
210         {
211                 .procname       = "nf_conntrack_udplite_timeout",
212                 .maxlen         = sizeof(unsigned int),
213                 .mode           = 0644,
214                 .proc_handler   = proc_dointvec_jiffies,
215         },
216         {
217                 .procname       = "nf_conntrack_udplite_timeout_stream",
218                 .maxlen         = sizeof(unsigned int),
219                 .mode           = 0644,
220                 .proc_handler   = proc_dointvec_jiffies,
221         },
222         { }
223 };
224 #endif /* CONFIG_SYSCTL */
225
226 static int udplite_kmemdup_sysctl_table(struct nf_proto_net *pn,
227                                         struct nf_udplite_net *un)
228 {
229 #ifdef CONFIG_SYSCTL
230         if (pn->ctl_table)
231                 return 0;
232
233         pn->ctl_table = kmemdup(udplite_sysctl_table,
234                                 sizeof(udplite_sysctl_table),
235                                 GFP_KERNEL);
236         if (!pn->ctl_table)
237                 return -ENOMEM;
238
239         pn->ctl_table[0].data = &un->timeouts[UDPLITE_CT_UNREPLIED];
240         pn->ctl_table[1].data = &un->timeouts[UDPLITE_CT_REPLIED];
241 #endif
242         return 0;
243 }
244
245 static int udplite_init_net(struct net *net, u_int16_t proto)
246 {
247         struct nf_udplite_net *un = udplite_pernet(net);
248         struct nf_proto_net *pn = &un->pn;
249
250         if (!pn->users) {
251                 int i;
252
253                 for (i = 0 ; i < UDPLITE_CT_MAX; i++)
254                         un->timeouts[i] = udplite_timeouts[i];
255         }
256
257         return udplite_kmemdup_sysctl_table(pn, un);
258 }
259
260 struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly =
261 {
262         .l3proto                = PF_INET,
263         .l4proto                = IPPROTO_UDPLITE,
264         .name                   = "udplite",
265         .allow_clash            = true,
266         .pkt_to_tuple           = udplite_pkt_to_tuple,
267         .invert_tuple           = udplite_invert_tuple,
268         .print_tuple            = udplite_print_tuple,
269         .packet                 = udplite_packet,
270         .get_timeouts           = udplite_get_timeouts,
271         .new                    = udplite_new,
272         .error                  = udplite_error,
273 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
274         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
275         .nlattr_tuple_size      = nf_ct_port_nlattr_tuple_size,
276         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
277         .nla_policy             = nf_ct_port_nla_policy,
278 #endif
279 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
280         .ctnl_timeout           = {
281                 .nlattr_to_obj  = udplite_timeout_nlattr_to_obj,
282                 .obj_to_nlattr  = udplite_timeout_obj_to_nlattr,
283                 .nlattr_max     = CTA_TIMEOUT_UDPLITE_MAX,
284                 .obj_size       = sizeof(unsigned int) *
285                                         CTA_TIMEOUT_UDPLITE_MAX,
286                 .nla_policy     = udplite_timeout_nla_policy,
287         },
288 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
289         .init_net               = udplite_init_net,
290 };
291 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udplite4);
292
293 struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
294 {
295         .l3proto                = PF_INET6,
296         .l4proto                = IPPROTO_UDPLITE,
297         .name                   = "udplite",
298         .allow_clash            = true,
299         .pkt_to_tuple           = udplite_pkt_to_tuple,
300         .invert_tuple           = udplite_invert_tuple,
301         .print_tuple            = udplite_print_tuple,
302         .packet                 = udplite_packet,
303         .get_timeouts           = udplite_get_timeouts,
304         .new                    = udplite_new,
305         .error                  = udplite_error,
306 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
307         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
308         .nlattr_tuple_size      = nf_ct_port_nlattr_tuple_size,
309         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
310         .nla_policy             = nf_ct_port_nla_policy,
311 #endif
312 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
313         .ctnl_timeout           = {
314                 .nlattr_to_obj  = udplite_timeout_nlattr_to_obj,
315                 .obj_to_nlattr  = udplite_timeout_obj_to_nlattr,
316                 .nlattr_max     = CTA_TIMEOUT_UDPLITE_MAX,
317                 .obj_size       = sizeof(unsigned int) *
318                                         CTA_TIMEOUT_UDPLITE_MAX,
319                 .nla_policy     = udplite_timeout_nla_policy,
320         },
321 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
322         .init_net               = udplite_init_net,
323 };
324 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udplite6);