4209ddb8fbafb0c814ae2d19c34e43c3f536786c
[linux-2.6.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/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 #endif
209 #ifdef CONFIG_SYSCTL
210         .ctl_table_users        = &udplite_sysctl_table_users,
211         .ctl_table_header       = &udplite_sysctl_header,
212         .ctl_table              = udplite_sysctl_table,
213 #endif
214 };
215
216 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
217 {
218         .l3proto                = PF_INET6,
219         .l4proto                = IPPROTO_UDPLITE,
220         .name                   = "udplite",
221         .pkt_to_tuple           = udplite_pkt_to_tuple,
222         .invert_tuple           = udplite_invert_tuple,
223         .print_tuple            = udplite_print_tuple,
224         .print_conntrack        = udplite_print_conntrack,
225         .packet                 = udplite_packet,
226         .new                    = udplite_new,
227         .error                  = udplite_error,
228 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
229         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
230         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
231 #endif
232 #ifdef CONFIG_SYSCTL
233         .ctl_table_users        = &udplite_sysctl_table_users,
234         .ctl_table_header       = &udplite_sysctl_header,
235         .ctl_table              = udplite_sysctl_table,
236 #endif
237 };
238
239 static int __init nf_conntrack_proto_udplite_init(void)
240 {
241         int err;
242
243         err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite4);
244         if (err < 0)
245                 goto err1;
246         err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite6);
247         if (err < 0)
248                 goto err2;
249         return 0;
250 err2:
251         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
252 err1:
253         return err;
254 }
255
256 static void __exit nf_conntrack_proto_udplite_exit(void)
257 {
258         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite6);
259         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
260 }
261
262 module_init(nf_conntrack_proto_udplite_init);
263 module_exit(nf_conntrack_proto_udplite_exit);
264
265 MODULE_LICENSE("GPL");