]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/netfilter/nf_conntrack_proto_udplite.c
[NETFILTER]: nf_conntrack: make print_conntrack function optional for l4protos
[linux-2.6-omap-h63xx.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 /* Returns verdict for packet, and may modify conntracktype */
63 static int udplite_packet(struct nf_conn *conntrack,
64                           const struct sk_buff *skb,
65                           unsigned int dataoff,
66                           enum ip_conntrack_info ctinfo,
67                           int pf,
68                           unsigned int hooknum)
69 {
70         /* If we've seen traffic both ways, this is some kind of UDP
71            stream.  Extend timeout. */
72         if (test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) {
73                 nf_ct_refresh_acct(conntrack, ctinfo, skb,
74                                    nf_ct_udplite_timeout_stream);
75                 /* Also, more likely to be important, and not a probe */
76                 if (!test_and_set_bit(IPS_ASSURED_BIT, &conntrack->status))
77                         nf_conntrack_event_cache(IPCT_STATUS, skb);
78         } else
79                 nf_ct_refresh_acct(conntrack, ctinfo, skb,
80                                    nf_ct_udplite_timeout);
81
82         return NF_ACCEPT;
83 }
84
85 /* Called when a new connection for this protocol found. */
86 static int udplite_new(struct nf_conn *conntrack, const struct sk_buff *skb,
87                        unsigned int dataoff)
88 {
89         return 1;
90 }
91
92 static int udplite_error(struct sk_buff *skb, unsigned int dataoff,
93                          enum ip_conntrack_info *ctinfo,
94                          int pf,
95                          unsigned int hooknum)
96 {
97         unsigned int udplen = skb->len - dataoff;
98         struct udphdr _hdr, *hdr;
99         unsigned int cscov;
100
101         /* Header is too small? */
102         hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
103         if (hdr == NULL) {
104                 if (LOG_INVALID(IPPROTO_UDPLITE))
105                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
106                                       "nf_ct_udplite: short packet ");
107                 return -NF_ACCEPT;
108         }
109
110         cscov = ntohs(hdr->len);
111         if (cscov == 0)
112                 cscov = udplen;
113         else if (cscov < sizeof(*hdr) || cscov > udplen) {
114                 if (LOG_INVALID(IPPROTO_UDPLITE))
115                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
116                                 "nf_ct_udplite: invalid checksum coverage ");
117                 return -NF_ACCEPT;
118         }
119
120         /* UDPLITE mandates checksums */
121         if (!hdr->check) {
122                 if (LOG_INVALID(IPPROTO_UDPLITE))
123                         nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
124                                       "nf_ct_udplite: checksum missing ");
125                 return -NF_ACCEPT;
126         }
127
128         /* Checksum invalid? Ignore. */
129         if (nf_conntrack_checksum && !skb_csum_unnecessary(skb) &&
130             hooknum == NF_INET_PRE_ROUTING) {
131                 if (pf == PF_INET) {
132                         struct iphdr *iph = ip_hdr(skb);
133
134                         skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
135                                                        udplen, IPPROTO_UDPLITE, 0);
136                 } else {
137                         struct ipv6hdr *ipv6h = ipv6_hdr(skb);
138                         __wsum hsum = skb_checksum(skb, 0, dataoff, 0);
139
140                         skb->csum = ~csum_unfold(
141                                 csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
142                                                 udplen, IPPROTO_UDPLITE,
143                                                 csum_sub(0, hsum)));
144                 }
145
146                 skb->ip_summed = CHECKSUM_NONE;
147                 if (__skb_checksum_complete_head(skb, dataoff + cscov)) {
148                         if (LOG_INVALID(IPPROTO_UDPLITE))
149                                 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
150                                               "nf_ct_udplite: bad UDPLite "
151                                               "checksum ");
152                         return -NF_ACCEPT;
153                 }
154                 skb->ip_summed = CHECKSUM_UNNECESSARY;
155         }
156
157         return NF_ACCEPT;
158 }
159
160 #ifdef CONFIG_SYSCTL
161 static unsigned int udplite_sysctl_table_users;
162 static struct ctl_table_header *udplite_sysctl_header;
163 static struct ctl_table udplite_sysctl_table[] = {
164         {
165                 .ctl_name       = CTL_UNNUMBERED,
166                 .procname       = "nf_conntrack_udplite_timeout",
167                 .data           = &nf_ct_udplite_timeout,
168                 .maxlen         = sizeof(unsigned int),
169                 .mode           = 0644,
170                 .proc_handler   = &proc_dointvec_jiffies,
171         },
172         {
173                 .ctl_name       = CTL_UNNUMBERED,
174                 .procname       = "nf_conntrack_udplite_timeout_stream",
175                 .data           = &nf_ct_udplite_timeout_stream,
176                 .maxlen         = sizeof(unsigned int),
177                 .mode           = 0644,
178                 .proc_handler   = &proc_dointvec_jiffies,
179         },
180         {
181                 .ctl_name       = 0
182         }
183 };
184 #endif /* CONFIG_SYSCTL */
185
186 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly =
187 {
188         .l3proto                = PF_INET,
189         .l4proto                = IPPROTO_UDPLITE,
190         .name                   = "udplite",
191         .pkt_to_tuple           = udplite_pkt_to_tuple,
192         .invert_tuple           = udplite_invert_tuple,
193         .print_tuple            = udplite_print_tuple,
194         .packet                 = udplite_packet,
195         .new                    = udplite_new,
196         .error                  = udplite_error,
197 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
198         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
199         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
200         .nla_policy             = nf_ct_port_nla_policy,
201 #endif
202 #ifdef CONFIG_SYSCTL
203         .ctl_table_users        = &udplite_sysctl_table_users,
204         .ctl_table_header       = &udplite_sysctl_header,
205         .ctl_table              = udplite_sysctl_table,
206 #endif
207 };
208
209 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
210 {
211         .l3proto                = PF_INET6,
212         .l4proto                = IPPROTO_UDPLITE,
213         .name                   = "udplite",
214         .pkt_to_tuple           = udplite_pkt_to_tuple,
215         .invert_tuple           = udplite_invert_tuple,
216         .print_tuple            = udplite_print_tuple,
217         .packet                 = udplite_packet,
218         .new                    = udplite_new,
219         .error                  = udplite_error,
220 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
221         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
222         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
223         .nla_policy             = nf_ct_port_nla_policy,
224 #endif
225 #ifdef CONFIG_SYSCTL
226         .ctl_table_users        = &udplite_sysctl_table_users,
227         .ctl_table_header       = &udplite_sysctl_header,
228         .ctl_table              = udplite_sysctl_table,
229 #endif
230 };
231
232 static int __init nf_conntrack_proto_udplite_init(void)
233 {
234         int err;
235
236         err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite4);
237         if (err < 0)
238                 goto err1;
239         err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite6);
240         if (err < 0)
241                 goto err2;
242         return 0;
243 err2:
244         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
245 err1:
246         return err;
247 }
248
249 static void __exit nf_conntrack_proto_udplite_exit(void)
250 {
251         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite6);
252         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
253 }
254
255 module_init(nf_conntrack_proto_udplite_init);
256 module_exit(nf_conntrack_proto_udplite_exit);
257
258 MODULE_LICENSE("GPL");