Commit | Line | Data |
---|---|---|
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 |
27 | enum udplite_conntrack { |
28 | UDPLITE_CT_UNREPLIED, | |
29 | UDPLITE_CT_REPLIED, | |
30 | UDPLITE_CT_MAX | |
31 | }; | |
32 | ||
33 | static unsigned int udplite_timeouts[UDPLITE_CT_MAX] = { | |
34 | [UDPLITE_CT_UNREPLIED] = 30*HZ, | |
35 | [UDPLITE_CT_REPLIED] = 180*HZ, | |
36 | }; | |
59eecdfb | 37 | |
09f263cd JE |
38 | static 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 |
54 | static 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. */ | |
63 | static 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 | ||
2c8503f5 PNA |
71 | static unsigned int *udplite_get_timeouts(struct net *net) |
72 | { | |
73 | return udplite_timeouts; | |
74 | } | |
75 | ||
59eecdfb | 76 | /* Returns verdict for packet, and may modify conntracktype */ |
c88130bc | 77 | static int udplite_packet(struct nf_conn *ct, |
59eecdfb PM |
78 | const struct sk_buff *skb, |
79 | unsigned int dataoff, | |
80 | enum ip_conntrack_info ctinfo, | |
76108cea | 81 | u_int8_t pf, |
2c8503f5 PNA |
82 | unsigned int hooknum, |
83 | unsigned int *timeouts) | |
59eecdfb PM |
84 | { |
85 | /* If we've seen traffic both ways, this is some kind of UDP | |
86 | stream. Extend timeout. */ | |
c88130bc PM |
87 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { |
88 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
2c8503f5 | 89 | timeouts[UDPLITE_CT_REPLIED]); |
59eecdfb | 90 | /* Also, more likely to be important, and not a probe */ |
c88130bc | 91 | if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status)) |
858b3133 | 92 | nf_conntrack_event_cache(IPCT_ASSURED, ct); |
5a41db94 PNA |
93 | } else { |
94 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
2c8503f5 | 95 | timeouts[UDPLITE_CT_UNREPLIED]); |
5a41db94 | 96 | } |
59eecdfb PM |
97 | return NF_ACCEPT; |
98 | } | |
99 | ||
100 | /* Called when a new connection for this protocol found. */ | |
09f263cd | 101 | static bool udplite_new(struct nf_conn *ct, const struct sk_buff *skb, |
2c8503f5 | 102 | unsigned int dataoff, unsigned int *timeouts) |
59eecdfb | 103 | { |
09f263cd | 104 | return true; |
59eecdfb PM |
105 | } |
106 | ||
8fea97ec | 107 | static int udplite_error(struct net *net, struct nf_conn *tmpl, |
74c51a14 AD |
108 | struct sk_buff *skb, |
109 | unsigned int dataoff, | |
59eecdfb | 110 | enum ip_conntrack_info *ctinfo, |
76108cea | 111 | u_int8_t pf, |
59eecdfb PM |
112 | unsigned int hooknum) |
113 | { | |
114 | unsigned int udplen = skb->len - dataoff; | |
da3f13c9 JE |
115 | const struct udphdr *hdr; |
116 | struct udphdr _hdr; | |
59eecdfb PM |
117 | unsigned int cscov; |
118 | ||
119 | /* Header is too small? */ | |
120 | hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); | |
121 | if (hdr == NULL) { | |
c2a2c7e0 | 122 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
59eecdfb PM |
123 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
124 | "nf_ct_udplite: short packet "); | |
125 | return -NF_ACCEPT; | |
126 | } | |
127 | ||
128 | cscov = ntohs(hdr->len); | |
129 | if (cscov == 0) | |
130 | cscov = udplen; | |
131 | else if (cscov < sizeof(*hdr) || cscov > udplen) { | |
c2a2c7e0 | 132 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
59eecdfb PM |
133 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
134 | "nf_ct_udplite: invalid checksum coverage "); | |
135 | return -NF_ACCEPT; | |
136 | } | |
137 | ||
138 | /* UDPLITE mandates checksums */ | |
139 | if (!hdr->check) { | |
c2a2c7e0 | 140 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
59eecdfb PM |
141 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
142 | "nf_ct_udplite: checksum missing "); | |
143 | return -NF_ACCEPT; | |
144 | } | |
145 | ||
146 | /* Checksum invalid? Ignore. */ | |
c04d0552 | 147 | if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING && |
d63a6507 PM |
148 | nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_UDP, |
149 | pf)) { | |
c2a2c7e0 | 150 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
d63a6507 PM |
151 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
152 | "nf_ct_udplite: bad UDPLite checksum "); | |
153 | return -NF_ACCEPT; | |
59eecdfb PM |
154 | } |
155 | ||
156 | return NF_ACCEPT; | |
157 | } | |
158 | ||
50978462 PNA |
159 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
160 | ||
161 | #include <linux/netfilter/nfnetlink.h> | |
162 | #include <linux/netfilter/nfnetlink_cttimeout.h> | |
163 | ||
164 | static int udplite_timeout_nlattr_to_obj(struct nlattr *tb[], void *data) | |
165 | { | |
166 | unsigned int *timeouts = data; | |
167 | ||
168 | /* set default timeouts for UDPlite. */ | |
169 | timeouts[UDPLITE_CT_UNREPLIED] = udplite_timeouts[UDPLITE_CT_UNREPLIED]; | |
170 | timeouts[UDPLITE_CT_REPLIED] = udplite_timeouts[UDPLITE_CT_REPLIED]; | |
171 | ||
172 | if (tb[CTA_TIMEOUT_UDPLITE_UNREPLIED]) { | |
173 | timeouts[UDPLITE_CT_UNREPLIED] = | |
174 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_UNREPLIED])) * HZ; | |
175 | } | |
176 | if (tb[CTA_TIMEOUT_UDPLITE_REPLIED]) { | |
177 | timeouts[UDPLITE_CT_REPLIED] = | |
178 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_REPLIED])) * HZ; | |
179 | } | |
180 | return 0; | |
181 | } | |
182 | ||
183 | static int | |
184 | udplite_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data) | |
185 | { | |
186 | const unsigned int *timeouts = data; | |
187 | ||
3c60a17b DM |
188 | if (nla_put_be32(skb, CTA_TIMEOUT_UDPLITE_UNREPLIED, |
189 | htonl(timeouts[UDPLITE_CT_UNREPLIED] / HZ)) || | |
190 | nla_put_be32(skb, CTA_TIMEOUT_UDPLITE_REPLIED, | |
191 | htonl(timeouts[UDPLITE_CT_REPLIED] / HZ))) | |
192 | goto nla_put_failure; | |
50978462 PNA |
193 | return 0; |
194 | ||
195 | nla_put_failure: | |
196 | return -ENOSPC; | |
197 | } | |
198 | ||
199 | static const struct nla_policy | |
200 | udplite_timeout_nla_policy[CTA_TIMEOUT_UDPLITE_MAX+1] = { | |
201 | [CTA_TIMEOUT_UDPLITE_UNREPLIED] = { .type = NLA_U32 }, | |
202 | [CTA_TIMEOUT_UDPLITE_REPLIED] = { .type = NLA_U32 }, | |
203 | }; | |
204 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
205 | ||
59eecdfb PM |
206 | #ifdef CONFIG_SYSCTL |
207 | static unsigned int udplite_sysctl_table_users; | |
208 | static struct ctl_table_header *udplite_sysctl_header; | |
209 | static struct ctl_table udplite_sysctl_table[] = { | |
210 | { | |
59eecdfb | 211 | .procname = "nf_conntrack_udplite_timeout", |
5a41db94 | 212 | .data = &udplite_timeouts[UDPLITE_CT_UNREPLIED], |
59eecdfb PM |
213 | .maxlen = sizeof(unsigned int), |
214 | .mode = 0644, | |
6d9f239a | 215 | .proc_handler = proc_dointvec_jiffies, |
59eecdfb PM |
216 | }, |
217 | { | |
59eecdfb | 218 | .procname = "nf_conntrack_udplite_timeout_stream", |
5a41db94 | 219 | .data = &udplite_timeouts[UDPLITE_CT_REPLIED], |
59eecdfb PM |
220 | .maxlen = sizeof(unsigned int), |
221 | .mode = 0644, | |
6d9f239a | 222 | .proc_handler = proc_dointvec_jiffies, |
59eecdfb | 223 | }, |
f8572d8f | 224 | { } |
59eecdfb PM |
225 | }; |
226 | #endif /* CONFIG_SYSCTL */ | |
227 | ||
228 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly = | |
229 | { | |
230 | .l3proto = PF_INET, | |
231 | .l4proto = IPPROTO_UDPLITE, | |
232 | .name = "udplite", | |
233 | .pkt_to_tuple = udplite_pkt_to_tuple, | |
234 | .invert_tuple = udplite_invert_tuple, | |
235 | .print_tuple = udplite_print_tuple, | |
59eecdfb | 236 | .packet = udplite_packet, |
2c8503f5 | 237 | .get_timeouts = udplite_get_timeouts, |
59eecdfb PM |
238 | .new = udplite_new, |
239 | .error = udplite_error, | |
c0cd1156 | 240 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 | 241 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
a400c30e | 242 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
fdf70832 | 243 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
f73e924c | 244 | .nla_policy = nf_ct_port_nla_policy, |
59eecdfb | 245 | #endif |
50978462 PNA |
246 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
247 | .ctnl_timeout = { | |
248 | .nlattr_to_obj = udplite_timeout_nlattr_to_obj, | |
249 | .obj_to_nlattr = udplite_timeout_obj_to_nlattr, | |
250 | .nlattr_max = CTA_TIMEOUT_UDPLITE_MAX, | |
251 | .obj_size = sizeof(unsigned int) * | |
252 | CTA_TIMEOUT_UDPLITE_MAX, | |
253 | .nla_policy = udplite_timeout_nla_policy, | |
254 | }, | |
255 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
59eecdfb PM |
256 | #ifdef CONFIG_SYSCTL |
257 | .ctl_table_users = &udplite_sysctl_table_users, | |
258 | .ctl_table_header = &udplite_sysctl_header, | |
259 | .ctl_table = udplite_sysctl_table, | |
260 | #endif | |
261 | }; | |
262 | ||
263 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly = | |
264 | { | |
265 | .l3proto = PF_INET6, | |
266 | .l4proto = IPPROTO_UDPLITE, | |
267 | .name = "udplite", | |
268 | .pkt_to_tuple = udplite_pkt_to_tuple, | |
269 | .invert_tuple = udplite_invert_tuple, | |
270 | .print_tuple = udplite_print_tuple, | |
59eecdfb | 271 | .packet = udplite_packet, |
2c8503f5 | 272 | .get_timeouts = udplite_get_timeouts, |
59eecdfb PM |
273 | .new = udplite_new, |
274 | .error = udplite_error, | |
c0cd1156 | 275 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 | 276 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
5ff48294 | 277 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
fdf70832 | 278 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
f73e924c | 279 | .nla_policy = nf_ct_port_nla_policy, |
59eecdfb | 280 | #endif |
50978462 PNA |
281 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
282 | .ctnl_timeout = { | |
283 | .nlattr_to_obj = udplite_timeout_nlattr_to_obj, | |
284 | .obj_to_nlattr = udplite_timeout_obj_to_nlattr, | |
285 | .nlattr_max = CTA_TIMEOUT_UDPLITE_MAX, | |
286 | .obj_size = sizeof(unsigned int) * | |
287 | CTA_TIMEOUT_UDPLITE_MAX, | |
288 | .nla_policy = udplite_timeout_nla_policy, | |
289 | }, | |
290 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
59eecdfb PM |
291 | #ifdef CONFIG_SYSCTL |
292 | .ctl_table_users = &udplite_sysctl_table_users, | |
293 | .ctl_table_header = &udplite_sysctl_header, | |
294 | .ctl_table = udplite_sysctl_table, | |
295 | #endif | |
296 | }; | |
297 | ||
298 | static int __init nf_conntrack_proto_udplite_init(void) | |
299 | { | |
300 | int err; | |
301 | ||
2c352f44 | 302 | err = nf_conntrack_l4proto_register(&init_net, &nf_conntrack_l4proto_udplite4); |
59eecdfb PM |
303 | if (err < 0) |
304 | goto err1; | |
2c352f44 | 305 | err = nf_conntrack_l4proto_register(&init_net, &nf_conntrack_l4proto_udplite6); |
59eecdfb PM |
306 | if (err < 0) |
307 | goto err2; | |
308 | return 0; | |
309 | err2: | |
2c352f44 | 310 | nf_conntrack_l4proto_unregister(&init_net, &nf_conntrack_l4proto_udplite4); |
59eecdfb PM |
311 | err1: |
312 | return err; | |
313 | } | |
314 | ||
315 | static void __exit nf_conntrack_proto_udplite_exit(void) | |
316 | { | |
2c352f44 G |
317 | nf_conntrack_l4proto_unregister(&init_net, &nf_conntrack_l4proto_udplite6); |
318 | nf_conntrack_l4proto_unregister(&init_net, &nf_conntrack_l4proto_udplite4); | |
59eecdfb PM |
319 | } |
320 | ||
321 | module_init(nf_conntrack_proto_udplite_init); | |
322 | module_exit(nf_conntrack_proto_udplite_exit); | |
323 | ||
324 | MODULE_LICENSE("GPL"); |