netfilter: nf_ct_udp[lite]: convert UDP[lite] timeouts to array
[linux-2.6/btrfs-unstable.git] / net / netfilter / nf_conntrack_proto_udplite.c
blobe73071743e01a3103ed07d8b682abb6aa51b2cbd
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>
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 */
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>
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>
27 enum udplite_conntrack {
28 UDPLITE_CT_UNREPLIED,
29 UDPLITE_CT_REPLIED,
30 UDPLITE_CT_MAX
33 static unsigned int udplite_timeouts[UDPLITE_CT_MAX] = {
34 [UDPLITE_CT_UNREPLIED] = 30*HZ,
35 [UDPLITE_CT_REPLIED] = 180*HZ,
38 static bool udplite_pkt_to_tuple(const struct sk_buff *skb,
39 unsigned int dataoff,
40 struct nf_conntrack_tuple *tuple)
42 const struct udphdr *hp;
43 struct udphdr _hdr;
45 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
46 if (hp == NULL)
47 return false;
49 tuple->src.u.udp.port = hp->source;
50 tuple->dst.u.udp.port = hp->dest;
51 return true;
54 static bool udplite_invert_tuple(struct nf_conntrack_tuple *tuple,
55 const struct nf_conntrack_tuple *orig)
57 tuple->src.u.udp.port = orig->dst.u.udp.port;
58 tuple->dst.u.udp.port = orig->src.u.udp.port;
59 return true;
62 /* Print out the per-protocol part of the tuple. */
63 static int udplite_print_tuple(struct seq_file *s,
64 const struct nf_conntrack_tuple *tuple)
66 return seq_printf(s, "sport=%hu dport=%hu ",
67 ntohs(tuple->src.u.udp.port),
68 ntohs(tuple->dst.u.udp.port));
71 /* Returns verdict for packet, and may modify conntracktype */
72 static int udplite_packet(struct nf_conn *ct,
73 const struct sk_buff *skb,
74 unsigned int dataoff,
75 enum ip_conntrack_info ctinfo,
76 u_int8_t pf,
77 unsigned int hooknum)
79 /* If we've seen traffic both ways, this is some kind of UDP
80 stream. Extend timeout. */
81 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
82 nf_ct_refresh_acct(ct, ctinfo, skb,
83 udplite_timeouts[UDPLITE_CT_REPLIED]);
84 /* Also, more likely to be important, and not a probe */
85 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
86 nf_conntrack_event_cache(IPCT_ASSURED, ct);
87 } else {
88 nf_ct_refresh_acct(ct, ctinfo, skb,
89 udplite_timeouts[UDPLITE_CT_UNREPLIED]);
91 return NF_ACCEPT;
94 /* Called when a new connection for this protocol found. */
95 static bool udplite_new(struct nf_conn *ct, const struct sk_buff *skb,
96 unsigned int dataoff)
98 return true;
101 static int udplite_error(struct net *net, struct nf_conn *tmpl,
102 struct sk_buff *skb,
103 unsigned int dataoff,
104 enum ip_conntrack_info *ctinfo,
105 u_int8_t pf,
106 unsigned int hooknum)
108 unsigned int udplen = skb->len - dataoff;
109 const struct udphdr *hdr;
110 struct udphdr _hdr;
111 unsigned int cscov;
113 /* Header is too small? */
114 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
115 if (hdr == NULL) {
116 if (LOG_INVALID(net, IPPROTO_UDPLITE))
117 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
118 "nf_ct_udplite: short packet ");
119 return -NF_ACCEPT;
122 cscov = ntohs(hdr->len);
123 if (cscov == 0)
124 cscov = udplen;
125 else if (cscov < sizeof(*hdr) || cscov > udplen) {
126 if (LOG_INVALID(net, IPPROTO_UDPLITE))
127 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
128 "nf_ct_udplite: invalid checksum coverage ");
129 return -NF_ACCEPT;
132 /* UDPLITE mandates checksums */
133 if (!hdr->check) {
134 if (LOG_INVALID(net, IPPROTO_UDPLITE))
135 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
136 "nf_ct_udplite: checksum missing ");
137 return -NF_ACCEPT;
140 /* Checksum invalid? Ignore. */
141 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
142 nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_UDP,
143 pf)) {
144 if (LOG_INVALID(net, IPPROTO_UDPLITE))
145 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
146 "nf_ct_udplite: bad UDPLite checksum ");
147 return -NF_ACCEPT;
150 return NF_ACCEPT;
153 #ifdef CONFIG_SYSCTL
154 static unsigned int udplite_sysctl_table_users;
155 static struct ctl_table_header *udplite_sysctl_header;
156 static struct ctl_table udplite_sysctl_table[] = {
158 .procname = "nf_conntrack_udplite_timeout",
159 .data = &udplite_timeouts[UDPLITE_CT_UNREPLIED],
160 .maxlen = sizeof(unsigned int),
161 .mode = 0644,
162 .proc_handler = proc_dointvec_jiffies,
165 .procname = "nf_conntrack_udplite_timeout_stream",
166 .data = &udplite_timeouts[UDPLITE_CT_REPLIED],
167 .maxlen = sizeof(unsigned int),
168 .mode = 0644,
169 .proc_handler = proc_dointvec_jiffies,
173 #endif /* CONFIG_SYSCTL */
175 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly =
177 .l3proto = PF_INET,
178 .l4proto = IPPROTO_UDPLITE,
179 .name = "udplite",
180 .pkt_to_tuple = udplite_pkt_to_tuple,
181 .invert_tuple = udplite_invert_tuple,
182 .print_tuple = udplite_print_tuple,
183 .packet = udplite_packet,
184 .new = udplite_new,
185 .error = udplite_error,
186 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
187 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
188 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
189 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
190 .nla_policy = nf_ct_port_nla_policy,
191 #endif
192 #ifdef CONFIG_SYSCTL
193 .ctl_table_users = &udplite_sysctl_table_users,
194 .ctl_table_header = &udplite_sysctl_header,
195 .ctl_table = udplite_sysctl_table,
196 #endif
199 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
201 .l3proto = PF_INET6,
202 .l4proto = IPPROTO_UDPLITE,
203 .name = "udplite",
204 .pkt_to_tuple = udplite_pkt_to_tuple,
205 .invert_tuple = udplite_invert_tuple,
206 .print_tuple = udplite_print_tuple,
207 .packet = udplite_packet,
208 .new = udplite_new,
209 .error = udplite_error,
210 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
211 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
212 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
213 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
214 .nla_policy = nf_ct_port_nla_policy,
215 #endif
216 #ifdef CONFIG_SYSCTL
217 .ctl_table_users = &udplite_sysctl_table_users,
218 .ctl_table_header = &udplite_sysctl_header,
219 .ctl_table = udplite_sysctl_table,
220 #endif
223 static int __init nf_conntrack_proto_udplite_init(void)
225 int err;
227 err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite4);
228 if (err < 0)
229 goto err1;
230 err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite6);
231 if (err < 0)
232 goto err2;
233 return 0;
234 err2:
235 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
236 err1:
237 return err;
240 static void __exit nf_conntrack_proto_udplite_exit(void)
242 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite6);
243 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
246 module_init(nf_conntrack_proto_udplite_init);
247 module_exit(nf_conntrack_proto_udplite_exit);
249 MODULE_LICENSE("GPL");