[NETFILTER]: Introduce NF_INET_ hook values
[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             hooknum == NF_INET_PRE_ROUTING) {
137                 if (pf == PF_INET) {
138                         struct iphdr *iph = ip_hdr(skb);
139
140                         skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
141                                                        udplen, IPPROTO_UDPLITE, 0);
142                 } else {
143                         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
144                         __wsum hsum = skb_checksum(skb, 0, dataoff, 0);
145
146                         skb->csum = ~csum_unfold(
147                                 csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
148                                                 udplen, IPPROTO_UDPLITE,
149                                                 csum_sub(0, hsum)));
150                 }
151
152                 skb->ip_summed = CHECKSUM_NONE;
153                 if (__skb_checksum_complete_head(skb, dataoff + cscov)) {
154                         if (LOG_INVALID(IPPROTO_UDPLITE))
155                                 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
156                                               "nf_ct_udplite: bad UDPLite "
157                                               "checksum ");
158                         return -NF_ACCEPT;
159                 }
160                 skb->ip_summed = CHECKSUM_UNNECESSARY;
161         }
162
163         return NF_ACCEPT;
164 }
165
166 #ifdef CONFIG_SYSCTL
167 static unsigned int udplite_sysctl_table_users;
168 static struct ctl_table_header *udplite_sysctl_header;
169 static struct ctl_table udplite_sysctl_table[] = {
170         {
171                 .ctl_name       = CTL_UNNUMBERED,
172                 .procname       = "nf_conntrack_udplite_timeout",
173                 .data           = &nf_ct_udplite_timeout,
174                 .maxlen         = sizeof(unsigned int),
175                 .mode           = 0644,
176                 .proc_handler   = &proc_dointvec_jiffies,
177         },
178         {
179                 .ctl_name       = CTL_UNNUMBERED,
180                 .procname       = "nf_conntrack_udplite_timeout_stream",
181                 .data           = &nf_ct_udplite_timeout_stream,
182                 .maxlen         = sizeof(unsigned int),
183                 .mode           = 0644,
184                 .proc_handler   = &proc_dointvec_jiffies,
185         },
186         {
187                 .ctl_name       = 0
188         }
189 };
190 #endif /* CONFIG_SYSCTL */
191
192 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly =
193 {
194         .l3proto                = PF_INET,
195         .l4proto                = IPPROTO_UDPLITE,
196         .name                   = "udplite",
197         .pkt_to_tuple           = udplite_pkt_to_tuple,
198         .invert_tuple           = udplite_invert_tuple,
199         .print_tuple            = udplite_print_tuple,
200         .print_conntrack        = udplite_print_conntrack,
201         .packet                 = udplite_packet,
202         .new                    = udplite_new,
203         .error                  = udplite_error,
204 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
205         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
206         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
207         .nla_policy             = nf_ct_port_nla_policy,
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         .nla_policy             = nf_ct_port_nla_policy,
232 #endif
233 #ifdef CONFIG_SYSCTL
234         .ctl_table_users        = &udplite_sysctl_table_users,
235         .ctl_table_header       = &udplite_sysctl_header,
236         .ctl_table              = udplite_sysctl_table,
237 #endif
238 };
239
240 static int __init nf_conntrack_proto_udplite_init(void)
241 {
242         int err;
243
244         err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite4);
245         if (err < 0)
246                 goto err1;
247         err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite6);
248         if (err < 0)
249                 goto err2;
250         return 0;
251 err2:
252         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
253 err1:
254         return err;
255 }
256
257 static void __exit nf_conntrack_proto_udplite_exit(void)
258 {
259         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite6);
260         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
261 }
262
263 module_init(nf_conntrack_proto_udplite_init);
264 module_exit(nf_conntrack_proto_udplite_exit);
265
266 MODULE_LICENSE("GPL");