netfilter: nf_ct_gre: add unsigned int array to define timeouts
[deliverable/linux.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 26
5a41db94
PNA
27enum udplite_conntrack {
28 UDPLITE_CT_UNREPLIED,
29 UDPLITE_CT_REPLIED,
30 UDPLITE_CT_MAX
31};
32
33static unsigned int udplite_timeouts[UDPLITE_CT_MAX] = {
34 [UDPLITE_CT_UNREPLIED] = 30*HZ,
35 [UDPLITE_CT_REPLIED] = 180*HZ,
36};
59eecdfb 37
09f263cd
JE
38static bool udplite_pkt_to_tuple(const struct sk_buff *skb,
39 unsigned int dataoff,
40 struct nf_conntrack_tuple *tuple)
59eecdfb 41{
da3f13c9
JE
42 const struct udphdr *hp;
43 struct udphdr _hdr;
59eecdfb
PM
44
45 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
46 if (hp == NULL)
09f263cd 47 return false;
59eecdfb
PM
48
49 tuple->src.u.udp.port = hp->source;
50 tuple->dst.u.udp.port = hp->dest;
09f263cd 51 return true;
59eecdfb
PM
52}
53
09f263cd
JE
54static bool udplite_invert_tuple(struct nf_conntrack_tuple *tuple,
55 const struct nf_conntrack_tuple *orig)
59eecdfb
PM
56{
57 tuple->src.u.udp.port = orig->dst.u.udp.port;
58 tuple->dst.u.udp.port = orig->src.u.udp.port;
09f263cd 59 return true;
59eecdfb
PM
60}
61
62/* Print out the per-protocol part of the tuple. */
63static int udplite_print_tuple(struct seq_file *s,
64 const struct nf_conntrack_tuple *tuple)
65{
66 return seq_printf(s, "sport=%hu dport=%hu ",
67 ntohs(tuple->src.u.udp.port),
68 ntohs(tuple->dst.u.udp.port));
69}
70
59eecdfb 71/* Returns verdict for packet, and may modify conntracktype */
c88130bc 72static int udplite_packet(struct nf_conn *ct,
59eecdfb
PM
73 const struct sk_buff *skb,
74 unsigned int dataoff,
75 enum ip_conntrack_info ctinfo,
76108cea 76 u_int8_t pf,
59eecdfb
PM
77 unsigned int hooknum)
78{
79 /* If we've seen traffic both ways, this is some kind of UDP
80 stream. Extend timeout. */
c88130bc
PM
81 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
82 nf_ct_refresh_acct(ct, ctinfo, skb,
5a41db94 83 udplite_timeouts[UDPLITE_CT_REPLIED]);
59eecdfb 84 /* Also, more likely to be important, and not a probe */
c88130bc 85 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
858b3133 86 nf_conntrack_event_cache(IPCT_ASSURED, ct);
5a41db94
PNA
87 } else {
88 nf_ct_refresh_acct(ct, ctinfo, skb,
89 udplite_timeouts[UDPLITE_CT_UNREPLIED]);
90 }
59eecdfb
PM
91 return NF_ACCEPT;
92}
93
94/* Called when a new connection for this protocol found. */
09f263cd
JE
95static bool udplite_new(struct nf_conn *ct, const struct sk_buff *skb,
96 unsigned int dataoff)
59eecdfb 97{
09f263cd 98 return true;
59eecdfb
PM
99}
100
8fea97ec 101static int udplite_error(struct net *net, struct nf_conn *tmpl,
74c51a14
AD
102 struct sk_buff *skb,
103 unsigned int dataoff,
59eecdfb 104 enum ip_conntrack_info *ctinfo,
76108cea 105 u_int8_t pf,
59eecdfb
PM
106 unsigned int hooknum)
107{
108 unsigned int udplen = skb->len - dataoff;
da3f13c9
JE
109 const struct udphdr *hdr;
110 struct udphdr _hdr;
59eecdfb
PM
111 unsigned int cscov;
112
113 /* Header is too small? */
114 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
115 if (hdr == NULL) {
c2a2c7e0 116 if (LOG_INVALID(net, IPPROTO_UDPLITE))
59eecdfb
PM
117 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
118 "nf_ct_udplite: short packet ");
119 return -NF_ACCEPT;
120 }
121
122 cscov = ntohs(hdr->len);
123 if (cscov == 0)
124 cscov = udplen;
125 else if (cscov < sizeof(*hdr) || cscov > udplen) {
c2a2c7e0 126 if (LOG_INVALID(net, IPPROTO_UDPLITE))
59eecdfb
PM
127 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
128 "nf_ct_udplite: invalid checksum coverage ");
129 return -NF_ACCEPT;
130 }
131
132 /* UDPLITE mandates checksums */
133 if (!hdr->check) {
c2a2c7e0 134 if (LOG_INVALID(net, IPPROTO_UDPLITE))
59eecdfb
PM
135 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
136 "nf_ct_udplite: checksum missing ");
137 return -NF_ACCEPT;
138 }
139
140 /* Checksum invalid? Ignore. */
c04d0552 141 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
d63a6507
PM
142 nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_UDP,
143 pf)) {
c2a2c7e0 144 if (LOG_INVALID(net, IPPROTO_UDPLITE))
d63a6507
PM
145 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
146 "nf_ct_udplite: bad UDPLite checksum ");
147 return -NF_ACCEPT;
59eecdfb
PM
148 }
149
150 return NF_ACCEPT;
151}
152
153#ifdef CONFIG_SYSCTL
154static unsigned int udplite_sysctl_table_users;
155static struct ctl_table_header *udplite_sysctl_header;
156static struct ctl_table udplite_sysctl_table[] = {
157 {
59eecdfb 158 .procname = "nf_conntrack_udplite_timeout",
5a41db94 159 .data = &udplite_timeouts[UDPLITE_CT_UNREPLIED],
59eecdfb
PM
160 .maxlen = sizeof(unsigned int),
161 .mode = 0644,
6d9f239a 162 .proc_handler = proc_dointvec_jiffies,
59eecdfb
PM
163 },
164 {
59eecdfb 165 .procname = "nf_conntrack_udplite_timeout_stream",
5a41db94 166 .data = &udplite_timeouts[UDPLITE_CT_REPLIED],
59eecdfb
PM
167 .maxlen = sizeof(unsigned int),
168 .mode = 0644,
6d9f239a 169 .proc_handler = proc_dointvec_jiffies,
59eecdfb 170 },
f8572d8f 171 { }
59eecdfb
PM
172};
173#endif /* CONFIG_SYSCTL */
174
175static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly =
176{
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,
59eecdfb
PM
183 .packet = udplite_packet,
184 .new = udplite_new,
185 .error = udplite_error,
c0cd1156 186#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
fdf70832 187 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
a400c30e 188 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
fdf70832 189 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
f73e924c 190 .nla_policy = nf_ct_port_nla_policy,
59eecdfb
PM
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
197};
198
199static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
200{
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,
59eecdfb
PM
207 .packet = udplite_packet,
208 .new = udplite_new,
209 .error = udplite_error,
c0cd1156 210#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
fdf70832 211 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
5ff48294 212 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
fdf70832 213 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
f73e924c 214 .nla_policy = nf_ct_port_nla_policy,
59eecdfb
PM
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
221};
222
223static int __init nf_conntrack_proto_udplite_init(void)
224{
225 int err;
226
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;
234err2:
235 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
236err1:
237 return err;
238}
239
240static void __exit nf_conntrack_proto_udplite_exit(void)
241{
242 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite6);
243 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
244}
245
246module_init(nf_conntrack_proto_udplite_init);
247module_exit(nf_conntrack_proto_udplite_exit);
248
249MODULE_LICENSE("GPL");
This page took 0.388228 seconds and 5 git commands to generate.