Merge tag 'cleanup-initcall' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[pandora-kernel.git] / net / netfilter / nf_conntrack_proto_udp.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/timer.h>
11 #include <linux/module.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 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
26 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
27
28 enum udp_conntrack {
29         UDP_CT_UNREPLIED,
30         UDP_CT_REPLIED,
31         UDP_CT_MAX
32 };
33
34 static unsigned int udp_timeouts[UDP_CT_MAX] = {
35         [UDP_CT_UNREPLIED]      = 30*HZ,
36         [UDP_CT_REPLIED]        = 180*HZ,
37 };
38
39 static bool udp_pkt_to_tuple(const struct sk_buff *skb,
40                              unsigned int dataoff,
41                              struct nf_conntrack_tuple *tuple)
42 {
43         const struct udphdr *hp;
44         struct udphdr _hdr;
45
46         /* Actually only need first 8 bytes. */
47         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
48         if (hp == NULL)
49                 return false;
50
51         tuple->src.u.udp.port = hp->source;
52         tuple->dst.u.udp.port = hp->dest;
53
54         return true;
55 }
56
57 static bool udp_invert_tuple(struct nf_conntrack_tuple *tuple,
58                              const struct nf_conntrack_tuple *orig)
59 {
60         tuple->src.u.udp.port = orig->dst.u.udp.port;
61         tuple->dst.u.udp.port = orig->src.u.udp.port;
62         return true;
63 }
64
65 /* Print out the per-protocol part of the tuple. */
66 static int udp_print_tuple(struct seq_file *s,
67                            const struct nf_conntrack_tuple *tuple)
68 {
69         return seq_printf(s, "sport=%hu dport=%hu ",
70                           ntohs(tuple->src.u.udp.port),
71                           ntohs(tuple->dst.u.udp.port));
72 }
73
74 static unsigned int *udp_get_timeouts(struct net *net)
75 {
76         return udp_timeouts;
77 }
78
79 /* Returns verdict for packet, and may modify conntracktype */
80 static int udp_packet(struct nf_conn *ct,
81                       const struct sk_buff *skb,
82                       unsigned int dataoff,
83                       enum ip_conntrack_info ctinfo,
84                       u_int8_t pf,
85                       unsigned int hooknum,
86                       unsigned int *timeouts)
87 {
88         /* If we've seen traffic both ways, this is some kind of UDP
89            stream.  Extend timeout. */
90         if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
91                 nf_ct_refresh_acct(ct, ctinfo, skb,
92                                    timeouts[UDP_CT_REPLIED]);
93                 /* Also, more likely to be important, and not a probe */
94                 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
95                         nf_conntrack_event_cache(IPCT_ASSURED, ct);
96         } else {
97                 nf_ct_refresh_acct(ct, ctinfo, skb,
98                                    timeouts[UDP_CT_UNREPLIED]);
99         }
100         return NF_ACCEPT;
101 }
102
103 /* Called when a new connection for this protocol found. */
104 static bool udp_new(struct nf_conn *ct, const struct sk_buff *skb,
105                     unsigned int dataoff, unsigned int *timeouts)
106 {
107         return true;
108 }
109
110 static int udp_error(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
111                      unsigned int dataoff, enum ip_conntrack_info *ctinfo,
112                      u_int8_t pf,
113                      unsigned int hooknum)
114 {
115         unsigned int udplen = skb->len - dataoff;
116         const struct udphdr *hdr;
117         struct udphdr _hdr;
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_UDP))
123                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
124                                       "nf_ct_udp: short packet ");
125                 return -NF_ACCEPT;
126         }
127
128         /* Truncated/malformed packets */
129         if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
130                 if (LOG_INVALID(net, IPPROTO_UDP))
131                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
132                                 "nf_ct_udp: truncated/malformed packet ");
133                 return -NF_ACCEPT;
134         }
135
136         /* Packet with no checksum */
137         if (!hdr->check)
138                 return NF_ACCEPT;
139
140         /* Checksum invalid? Ignore.
141          * We skip checking packets on the outgoing path
142          * because the checksum is assumed to be correct.
143          * FIXME: Source route IP option packets --RR */
144         if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
145             nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) {
146                 if (LOG_INVALID(net, IPPROTO_UDP))
147                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
148                                 "nf_ct_udp: bad UDP checksum ");
149                 return -NF_ACCEPT;
150         }
151
152         return NF_ACCEPT;
153 }
154
155 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
156
157 #include <linux/netfilter/nfnetlink.h>
158 #include <linux/netfilter/nfnetlink_cttimeout.h>
159
160 static int udp_timeout_nlattr_to_obj(struct nlattr *tb[], void *data)
161 {
162         unsigned int *timeouts = data;
163
164         /* set default timeouts for UDP. */
165         timeouts[UDP_CT_UNREPLIED] = udp_timeouts[UDP_CT_UNREPLIED];
166         timeouts[UDP_CT_REPLIED] = udp_timeouts[UDP_CT_REPLIED];
167
168         if (tb[CTA_TIMEOUT_UDP_UNREPLIED]) {
169                 timeouts[UDP_CT_UNREPLIED] =
170                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_UNREPLIED])) * HZ;
171         }
172         if (tb[CTA_TIMEOUT_UDP_REPLIED]) {
173                 timeouts[UDP_CT_REPLIED] =
174                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_REPLIED])) * HZ;
175         }
176         return 0;
177 }
178
179 static int
180 udp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
181 {
182         const unsigned int *timeouts = data;
183
184         if (nla_put_be32(skb, CTA_TIMEOUT_UDP_UNREPLIED,
185                          htonl(timeouts[UDP_CT_UNREPLIED] / HZ)) ||
186             nla_put_be32(skb, CTA_TIMEOUT_UDP_REPLIED,
187                          htonl(timeouts[UDP_CT_REPLIED] / HZ)))
188                 goto nla_put_failure;
189         return 0;
190
191 nla_put_failure:
192         return -ENOSPC;
193 }
194
195 static const struct nla_policy
196 udp_timeout_nla_policy[CTA_TIMEOUT_UDP_MAX+1] = {
197        [CTA_TIMEOUT_UDP_UNREPLIED]      = { .type = NLA_U32 },
198        [CTA_TIMEOUT_UDP_REPLIED]        = { .type = NLA_U32 },
199 };
200 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
201
202 #ifdef CONFIG_SYSCTL
203 static unsigned int udp_sysctl_table_users;
204 static struct ctl_table_header *udp_sysctl_header;
205 static struct ctl_table udp_sysctl_table[] = {
206         {
207                 .procname       = "nf_conntrack_udp_timeout",
208                 .data           = &udp_timeouts[UDP_CT_UNREPLIED],
209                 .maxlen         = sizeof(unsigned int),
210                 .mode           = 0644,
211                 .proc_handler   = proc_dointvec_jiffies,
212         },
213         {
214                 .procname       = "nf_conntrack_udp_timeout_stream",
215                 .data           = &udp_timeouts[UDP_CT_REPLIED],
216                 .maxlen         = sizeof(unsigned int),
217                 .mode           = 0644,
218                 .proc_handler   = proc_dointvec_jiffies,
219         },
220         { }
221 };
222 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
223 static struct ctl_table udp_compat_sysctl_table[] = {
224         {
225                 .procname       = "ip_conntrack_udp_timeout",
226                 .data           = &udp_timeouts[UDP_CT_UNREPLIED],
227                 .maxlen         = sizeof(unsigned int),
228                 .mode           = 0644,
229                 .proc_handler   = proc_dointvec_jiffies,
230         },
231         {
232                 .procname       = "ip_conntrack_udp_timeout_stream",
233                 .data           = &udp_timeouts[UDP_CT_REPLIED],
234                 .maxlen         = sizeof(unsigned int),
235                 .mode           = 0644,
236                 .proc_handler   = proc_dointvec_jiffies,
237         },
238         { }
239 };
240 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
241 #endif /* CONFIG_SYSCTL */
242
243 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly =
244 {
245         .l3proto                = PF_INET,
246         .l4proto                = IPPROTO_UDP,
247         .name                   = "udp",
248         .pkt_to_tuple           = udp_pkt_to_tuple,
249         .invert_tuple           = udp_invert_tuple,
250         .print_tuple            = udp_print_tuple,
251         .packet                 = udp_packet,
252         .get_timeouts           = udp_get_timeouts,
253         .new                    = udp_new,
254         .error                  = udp_error,
255 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
256         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
257         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
258         .nlattr_tuple_size      = nf_ct_port_nlattr_tuple_size,
259         .nla_policy             = nf_ct_port_nla_policy,
260 #endif
261 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
262         .ctnl_timeout           = {
263                 .nlattr_to_obj  = udp_timeout_nlattr_to_obj,
264                 .obj_to_nlattr  = udp_timeout_obj_to_nlattr,
265                 .nlattr_max     = CTA_TIMEOUT_UDP_MAX,
266                 .obj_size       = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
267                 .nla_policy     = udp_timeout_nla_policy,
268         },
269 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
270 #ifdef CONFIG_SYSCTL
271         .ctl_table_users        = &udp_sysctl_table_users,
272         .ctl_table_header       = &udp_sysctl_header,
273         .ctl_table              = udp_sysctl_table,
274 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
275         .ctl_compat_table       = udp_compat_sysctl_table,
276 #endif
277 #endif
278 };
279 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4);
280
281 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly =
282 {
283         .l3proto                = PF_INET6,
284         .l4proto                = IPPROTO_UDP,
285         .name                   = "udp",
286         .pkt_to_tuple           = udp_pkt_to_tuple,
287         .invert_tuple           = udp_invert_tuple,
288         .print_tuple            = udp_print_tuple,
289         .packet                 = udp_packet,
290         .get_timeouts           = udp_get_timeouts,
291         .new                    = udp_new,
292         .error                  = udp_error,
293 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
294         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
295         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
296         .nlattr_tuple_size      = nf_ct_port_nlattr_tuple_size,
297         .nla_policy             = nf_ct_port_nla_policy,
298 #endif
299 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
300         .ctnl_timeout           = {
301                 .nlattr_to_obj  = udp_timeout_nlattr_to_obj,
302                 .obj_to_nlattr  = udp_timeout_obj_to_nlattr,
303                 .nlattr_max     = CTA_TIMEOUT_UDP_MAX,
304                 .obj_size       = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
305                 .nla_policy     = udp_timeout_nla_policy,
306         },
307 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
308 #ifdef CONFIG_SYSCTL
309         .ctl_table_users        = &udp_sysctl_table_users,
310         .ctl_table_header       = &udp_sysctl_header,
311         .ctl_table              = udp_sysctl_table,
312 #endif
313 };
314 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6);