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