[NETFILTER]: nf_log: move logging stuff to seperate header
[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 #include <net/netfilter/nf_log.h>
26
27 static unsigned int nf_ct_udplite_timeout __read_mostly = 30*HZ;
28 static unsigned int nf_ct_udplite_timeout_stream __read_mostly = 180*HZ;
29
30 static int udplite_pkt_to_tuple(const struct sk_buff *skb,
31                                 unsigned int dataoff,
32                                 struct nf_conntrack_tuple *tuple)
33 {
34         struct udphdr _hdr, *hp;
35
36         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
37         if (hp == NULL)
38                 return 0;
39
40         tuple->src.u.udp.port = hp->source;
41         tuple->dst.u.udp.port = hp->dest;
42         return 1;
43 }
44
45 static int udplite_invert_tuple(struct nf_conntrack_tuple *tuple,
46                                 const struct nf_conntrack_tuple *orig)
47 {
48         tuple->src.u.udp.port = orig->dst.u.udp.port;
49         tuple->dst.u.udp.port = orig->src.u.udp.port;
50         return 1;
51 }
52
53 /* Print out the per-protocol part of the tuple. */
54 static int udplite_print_tuple(struct seq_file *s,
55                                const struct nf_conntrack_tuple *tuple)
56 {
57         return seq_printf(s, "sport=%hu dport=%hu ",
58                           ntohs(tuple->src.u.udp.port),
59                           ntohs(tuple->dst.u.udp.port));
60 }
61
62 /* Print out the private part of the conntrack. */
63 static int udplite_print_conntrack(struct seq_file *s,
64                                    const struct nf_conn *conntrack)
65 {
66         return 0;
67 }
68
69 /* Returns verdict for packet, and may modify conntracktype */
70 static int udplite_packet(struct nf_conn *conntrack,
71                           const struct sk_buff *skb,
72                           unsigned int dataoff,
73                           enum ip_conntrack_info ctinfo,
74                           int pf,
75                           unsigned int hooknum)
76 {
77         /* If we've seen traffic both ways, this is some kind of UDP
78            stream.  Extend timeout. */
79         if (test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
80                 nf_ct_refresh_acct(conntrack, ctinfo, skb,
81                                    nf_ct_udplite_timeout_stream);
82                 /* Also, more likely to be important, and not a probe */
83                 if (!test_and_set_bit(IPS_ASSURED_BIT, &conntrack->status))
84                         nf_conntrack_event_cache(IPCT_STATUS, skb);
85         } else
86                 nf_ct_refresh_acct(conntrack, ctinfo, skb,
87                                    nf_ct_udplite_timeout);
88
89         return NF_ACCEPT;
90 }
91
92 /* Called when a new connection for this protocol found. */
93 static int udplite_new(struct nf_conn *conntrack, const struct sk_buff *skb,
94                        unsigned int dataoff)
95 {
96         return 1;
97 }
98
99 static int udplite_error(struct sk_buff *skb, unsigned int dataoff,
100                          enum ip_conntrack_info *ctinfo,
101                          int pf,
102                          unsigned int hooknum)
103 {
104         unsigned int udplen = skb->len - dataoff;
105         struct udphdr _hdr, *hdr;
106         unsigned int cscov;
107
108         /* Header is too small? */
109         hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
110         if (hdr == NULL) {
111                 if (LOG_INVALID(IPPROTO_UDPLITE))
112                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
113                                       "nf_ct_udplite: short packet ");
114                 return -NF_ACCEPT;
115         }
116
117         cscov = ntohs(hdr->len);
118         if (cscov == 0)
119                 cscov = udplen;
120         else if (cscov < sizeof(*hdr) || cscov > udplen) {
121                 if (LOG_INVALID(IPPROTO_UDPLITE))
122                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
123                                 "nf_ct_udplite: invalid checksum coverage ");
124                 return -NF_ACCEPT;
125         }
126
127         /* UDPLITE mandates checksums */
128         if (!hdr->check) {
129                 if (LOG_INVALID(IPPROTO_UDPLITE))
130                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
131                                       "nf_ct_udplite: checksum missing ");
132                 return -NF_ACCEPT;
133         }
134
135         /* Checksum invalid? Ignore. */
136         if (nf_conntrack_checksum && !skb_csum_unnecessary(skb) &&
137             hooknum == NF_INET_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");