b8981dd922bed6efe20c2b2727e8e673410b4d20
[safe/jmp/linux-2.6] / 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/module.h>
13 #include <linux/udp.h>
14 #include <linux/seq_file.h>
15 #include <linux/skbuff.h>
16 #include <linux/ipv6.h>
17 #include <net/ip6_checksum.h>
18 #include <net/checksum.h>
19
20 #include <linux/netfilter.h>
21 #include <linux/netfilter_ipv4.h>
22 #include <linux/netfilter_ipv6.h>
23 #include <net/netfilter/nf_conntrack_l4proto.h>
24 #include <net/netfilter/nf_conntrack_ecache.h>
25
26 static unsigned int nf_ct_udplite_timeout __read_mostly = 30*HZ;
27 static unsigned int nf_ct_udplite_timeout_stream __read_mostly = 180*HZ;
28
29 static int udplite_pkt_to_tuple(const struct sk_buff *skb,
30                                 unsigned int dataoff,
31                                 struct nf_conntrack_tuple *tuple)
32 {
33         struct udphdr _hdr, *hp;
34
35         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
36         if (hp == NULL)
37                 return 0;
38
39         tuple->src.u.udp.port = hp->source;
40         tuple->dst.u.udp.port = hp->dest;
41         return 1;
42 }
43
44 static int udplite_invert_tuple(struct nf_conntrack_tuple *tuple,
45                                 const struct nf_conntrack_tuple *orig)
46 {
47         tuple->src.u.udp.port = orig->dst.u.udp.port;
48         tuple->dst.u.udp.port = orig->src.u.udp.port;
49         return 1;
50 }
51
52 /* Print out the per-protocol part of the tuple. */
53 static int udplite_print_tuple(struct seq_file *s,
54                                const struct nf_conntrack_tuple *tuple)
55 {
56         return seq_printf(s, "sport=%hu dport=%hu ",
57                           ntohs(tuple->src.u.udp.port),
58                           ntohs(tuple->dst.u.udp.port));
59 }
60
61 /* Print out the private part of the conntrack. */
62 static int udplite_print_conntrack(struct seq_file *s,
63                                    const struct nf_conn *conntrack)
64 {
65         return 0;
66 }
67
68 /* Returns verdict for packet, and may modify conntracktype */
69 static int udplite_packet(struct nf_conn *conntrack,
70                           const struct sk_buff *skb,
71                           unsigned int dataoff,
72                           enum ip_conntrack_info ctinfo,
73                           int pf,
74                           unsigned int hooknum)
75 {
76         /* If we've seen traffic both ways, this is some kind of UDP
77            stream.  Extend timeout. */
78         if (test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
79                 nf_ct_refresh_acct(conntrack, ctinfo, skb,
80                                    nf_ct_udplite_timeout_stream);
81                 /* Also, more likely to be important, and not a probe */
82                 if (!test_and_set_bit(IPS_ASSURED_BIT, &conntrack->status))
83                         nf_conntrack_event_cache(IPCT_STATUS, skb);
84         } else
85                 nf_ct_refresh_acct(conntrack, ctinfo, skb,
86                                    nf_ct_udplite_timeout);
87
88         return NF_ACCEPT;
89 }
90
91 /* Called when a new connection for this protocol found. */
92 static int udplite_new(struct nf_conn *conntrack, const struct sk_buff *skb,
93                        unsigned int dataoff)
94 {
95         return 1;
96 }
97
98 static int udplite_error(struct sk_buff *skb, unsigned int dataoff,
99                          enum ip_conntrack_info *ctinfo,
100                          int pf,
101                          unsigned int hooknum)
102 {
103         unsigned int udplen = skb->len - dataoff;
104         struct udphdr _hdr, *hdr;
105         unsigned int cscov;
106
107         /* Header is too small? */
108         hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
109         if (hdr == NULL) {
110                 if (LOG_INVALID(IPPROTO_UDPLITE))
111                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
112                                       "nf_ct_udplite: short packet ");
113                 return -NF_ACCEPT;
114         }
115
116         cscov = ntohs(hdr->len);
117         if (cscov == 0)
118                 cscov = udplen;
119         else if (cscov < sizeof(*hdr) || cscov > udplen) {
120                 if (LOG_INVALID(IPPROTO_UDPLITE))
121                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
122                                 "nf_ct_udplite: invalid checksum coverage ");
123                 return -NF_ACCEPT;
124         }
125
126         /* UDPLITE mandates checksums */
127         if (!hdr->check) {
128                 if (LOG_INVALID(IPPROTO_UDPLITE))
129                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
130                                       "nf_ct_udplite: checksum missing ");
131                 return -NF_ACCEPT;
132         }
133
134         /* Checksum invalid? Ignore. */
135         if (nf_conntrack_checksum && !skb_csum_unnecessary(skb) &&
136             ((pf == PF_INET && hooknum == NF_IP_PRE_ROUTING) ||
137              (pf == PF_INET6 && hooknum == NF_IP6_PRE_ROUTING))) {
138                 if (pf == PF_INET) {
139                         struct iphdr *iph = ip_hdr(skb);
140
141                         skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
142                                                        udplen, IPPROTO_UDPLITE, 0);
143                 } else {
144                         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
145                         __wsum hsum = skb_checksum(skb, 0, dataoff, 0);
146
147                         skb->csum = ~csum_unfold(
148                                 csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
149                                                 udplen, IPPROTO_UDPLITE,
150                                                 csum_sub(0, hsum)));
151                 }
152
153                 skb->ip_summed = CHECKSUM_NONE;
154                 if (__skb_checksum_complete_head(skb, dataoff + cscov)) {
155                         if (LOG_INVALID(IPPROTO_UDPLITE))
156                                 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
157                                               "nf_ct_udplite: bad UDPLite "
158                                               "checksum ");
159                         return -NF_ACCEPT;
160                 }
161                 skb->ip_summed = CHECKSUM_UNNECESSARY;
162         }
163
164         return NF_ACCEPT;
165 }
166
167 #ifdef CONFIG_SYSCTL
168 static unsigned int udplite_sysctl_table_users;
169 static struct ctl_table_header *udplite_sysctl_header;
170 static struct ctl_table udplite_sysctl_table[] = {
171         {
172                 .ctl_name       = CTL_UNNUMBERED,
173                 .procname       = "nf_conntrack_udplite_timeout",
174                 .data           = &nf_ct_udplite_timeout,
175                 .maxlen         = sizeof(unsigned int),
176                 .mode           = 0644,
177                 .proc_handler   = &proc_dointvec_jiffies,
178         },
179         {
180                 .ctl_name       = CTL_UNNUMBERED,
181                 .procname       = "nf_conntrack_udplite_timeout_stream",
182                 .data           = &nf_ct_udplite_timeout_stream,
183                 .maxlen         = sizeof(unsigned int),
184                 .mode           = 0644,
185                 .proc_handler   = &proc_dointvec_jiffies,
186         },
187         {
188                 .ctl_name       = 0
189         }
190 };
191 #endif /* CONFIG_SYSCTL */
192
193 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly =
194 {
195         .l3proto                = PF_INET,
196         .l4proto                = IPPROTO_UDPLITE,
197         .name                   = "udplite",
198         .pkt_to_tuple           = udplite_pkt_to_tuple,
199         .invert_tuple           = udplite_invert_tuple,
200         .print_tuple            = udplite_print_tuple,
201         .print_conntrack        = udplite_print_conntrack,
202         .packet                 = udplite_packet,
203         .new                    = udplite_new,
204         .error                  = udplite_error,
205 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
206         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
207         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
208         .nla_policy             = nf_ct_port_nla_policy,
209 #endif
210 #ifdef CONFIG_SYSCTL
211         .ctl_table_users        = &udplite_sysctl_table_users,
212         .ctl_table_header       = &udplite_sysctl_header,
213         .ctl_table              = udplite_sysctl_table,
214 #endif
215 };
216
217 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
218 {
219         .l3proto                = PF_INET6,
220         .l4proto                = IPPROTO_UDPLITE,
221         .name                   = "udplite",
222         .pkt_to_tuple           = udplite_pkt_to_tuple,
223         .invert_tuple           = udplite_invert_tuple,
224         .print_tuple            = udplite_print_tuple,
225         .print_conntrack        = udplite_print_conntrack,
226         .packet                 = udplite_packet,
227         .new                    = udplite_new,
228         .error                  = udplite_error,
229 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
230         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
231         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
232         .nla_policy             = nf_ct_port_nla_policy,
233 #endif
234 #ifdef CONFIG_SYSCTL
235         .ctl_table_users        = &udplite_sysctl_table_users,
236         .ctl_table_header       = &udplite_sysctl_header,
237         .ctl_table              = udplite_sysctl_table,
238 #endif
239 };
240
241 static int __init nf_conntrack_proto_udplite_init(void)
242 {
243         int err;
244
245         err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite4);
246         if (err < 0)
247                 goto err1;
248         err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite6);
249         if (err < 0)
250                 goto err2;
251         return 0;
252 err2:
253         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
254 err1:
255         return err;
256 }
257
258 static void __exit nf_conntrack_proto_udplite_exit(void)
259 {
260         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite6);
261         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
262 }
263
264 module_init(nf_conntrack_proto_udplite_init);
265 module_exit(nf_conntrack_proto_udplite_exit);
266
267 MODULE_LICENSE("GPL");