Commit | Line | Data |
---|---|---|
9fb9cbb1 YK |
1 | /* (C) 1999-2001 Paul `Rusty' Russell |
2 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> | |
f229f6ce | 3 | * (C) 2006-2012 Patrick McHardy <kaber@trash.net> |
9fb9cbb1 YK |
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. | |
9fb9cbb1 YK |
8 | */ |
9 | ||
10 | #include <linux/types.h> | |
9fb9cbb1 YK |
11 | #include <linux/timer.h> |
12 | #include <linux/module.h> | |
9fb9cbb1 YK |
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> | |
f6180121 | 19 | |
9fb9cbb1 YK |
20 | #include <linux/netfilter.h> |
21 | #include <linux/netfilter_ipv4.h> | |
22 | #include <linux/netfilter_ipv6.h> | |
605dcad6 | 23 | #include <net/netfilter/nf_conntrack_l4proto.h> |
f6180121 | 24 | #include <net/netfilter/nf_conntrack_ecache.h> |
f01ffbd6 | 25 | #include <net/netfilter/nf_log.h> |
9d2493f8 CP |
26 | #include <net/netfilter/ipv4/nf_conntrack_ipv4.h> |
27 | #include <net/netfilter/ipv6/nf_conntrack_ipv6.h> | |
9fb9cbb1 | 28 | |
5a41db94 PNA |
29 | static unsigned int udp_timeouts[UDP_CT_MAX] = { |
30 | [UDP_CT_UNREPLIED] = 30*HZ, | |
31 | [UDP_CT_REPLIED] = 180*HZ, | |
32 | }; | |
9fb9cbb1 | 33 | |
0ce490ad G |
34 | static inline struct nf_udp_net *udp_pernet(struct net *net) |
35 | { | |
36 | return &net->ct.nf_ct_proto.udp; | |
37 | } | |
38 | ||
09f263cd | 39 | static bool udp_pkt_to_tuple(const struct sk_buff *skb, |
9fb9cbb1 YK |
40 | unsigned int dataoff, |
41 | struct nf_conntrack_tuple *tuple) | |
42 | { | |
da3f13c9 JE |
43 | const struct udphdr *hp; |
44 | struct udphdr _hdr; | |
9fb9cbb1 YK |
45 | |
46 | /* Actually only need first 8 bytes. */ | |
47 | hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); | |
48 | if (hp == NULL) | |
09f263cd | 49 | return false; |
9fb9cbb1 YK |
50 | |
51 | tuple->src.u.udp.port = hp->source; | |
52 | tuple->dst.u.udp.port = hp->dest; | |
53 | ||
09f263cd | 54 | return true; |
9fb9cbb1 YK |
55 | } |
56 | ||
09f263cd JE |
57 | static bool udp_invert_tuple(struct nf_conntrack_tuple *tuple, |
58 | const struct nf_conntrack_tuple *orig) | |
9fb9cbb1 YK |
59 | { |
60 | tuple->src.u.udp.port = orig->dst.u.udp.port; | |
61 | tuple->dst.u.udp.port = orig->src.u.udp.port; | |
09f263cd | 62 | return true; |
9fb9cbb1 YK |
63 | } |
64 | ||
65 | /* Print out the per-protocol part of the tuple. */ | |
66 | static int udp_print_tuple(struct seq_file *s, | |
67 | const struct nf_conntrack_tuple *tuple) | |
68 | { | |
69 | return seq_printf(s, "sport=%hu dport=%hu ", | |
70 | ntohs(tuple->src.u.udp.port), | |
71 | ntohs(tuple->dst.u.udp.port)); | |
72 | } | |
73 | ||
2c8503f5 PNA |
74 | static unsigned int *udp_get_timeouts(struct net *net) |
75 | { | |
0ce490ad | 76 | return udp_pernet(net)->timeouts; |
2c8503f5 PNA |
77 | } |
78 | ||
9fb9cbb1 | 79 | /* Returns verdict for packet, and may modify conntracktype */ |
c88130bc | 80 | static int udp_packet(struct nf_conn *ct, |
9fb9cbb1 YK |
81 | const struct sk_buff *skb, |
82 | unsigned int dataoff, | |
83 | enum ip_conntrack_info ctinfo, | |
76108cea | 84 | u_int8_t pf, |
2c8503f5 PNA |
85 | unsigned int hooknum, |
86 | unsigned int *timeouts) | |
9fb9cbb1 YK |
87 | { |
88 | /* If we've seen traffic both ways, this is some kind of UDP | |
89 | stream. Extend timeout. */ | |
c88130bc | 90 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { |
5a41db94 | 91 | nf_ct_refresh_acct(ct, ctinfo, skb, |
2c8503f5 | 92 | timeouts[UDP_CT_REPLIED]); |
9fb9cbb1 | 93 | /* Also, more likely to be important, and not a probe */ |
c88130bc | 94 | if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status)) |
858b3133 | 95 | nf_conntrack_event_cache(IPCT_ASSURED, ct); |
5a41db94 PNA |
96 | } else { |
97 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
2c8503f5 | 98 | timeouts[UDP_CT_UNREPLIED]); |
5a41db94 | 99 | } |
9fb9cbb1 YK |
100 | return NF_ACCEPT; |
101 | } | |
102 | ||
103 | /* Called when a new connection for this protocol found. */ | |
09f263cd | 104 | static bool udp_new(struct nf_conn *ct, const struct sk_buff *skb, |
2c8503f5 | 105 | unsigned int dataoff, unsigned int *timeouts) |
9fb9cbb1 | 106 | { |
09f263cd | 107 | return true; |
9fb9cbb1 YK |
108 | } |
109 | ||
8fea97ec PM |
110 | static int udp_error(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb, |
111 | unsigned int dataoff, enum ip_conntrack_info *ctinfo, | |
76108cea | 112 | u_int8_t pf, |
96f6bf82 | 113 | unsigned int hooknum) |
9fb9cbb1 YK |
114 | { |
115 | unsigned int udplen = skb->len - dataoff; | |
da3f13c9 JE |
116 | const struct udphdr *hdr; |
117 | struct udphdr _hdr; | |
9fb9cbb1 YK |
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_UDP)) |
30e0c6a6 | 123 | nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL, |
9fb9cbb1 YK |
124 | "nf_ct_udp: short packet "); |
125 | return -NF_ACCEPT; | |
126 | } | |
127 | ||
128 | /* Truncated/malformed packets */ | |
129 | if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) { | |
c2a2c7e0 | 130 | if (LOG_INVALID(net, IPPROTO_UDP)) |
30e0c6a6 | 131 | nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL, |
9fb9cbb1 YK |
132 | "nf_ct_udp: truncated/malformed packet "); |
133 | return -NF_ACCEPT; | |
134 | } | |
135 | ||
136 | /* Packet with no checksum */ | |
137 | if (!hdr->check) | |
138 | return NF_ACCEPT; | |
139 | ||
140 | /* Checksum invalid? Ignore. | |
141 | * We skip checking packets on the outgoing path | |
84fa7933 | 142 | * because the checksum is assumed to be correct. |
9fb9cbb1 | 143 | * FIXME: Source route IP option packets --RR */ |
c04d0552 | 144 | if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING && |
96f6bf82 | 145 | nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) { |
c2a2c7e0 | 146 | if (LOG_INVALID(net, IPPROTO_UDP)) |
30e0c6a6 | 147 | nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL, |
9fb9cbb1 YK |
148 | "nf_ct_udp: bad UDP checksum "); |
149 | return -NF_ACCEPT; | |
150 | } | |
151 | ||
152 | return NF_ACCEPT; | |
153 | } | |
154 | ||
50978462 PNA |
155 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
156 | ||
157 | #include <linux/netfilter/nfnetlink.h> | |
158 | #include <linux/netfilter/nfnetlink_cttimeout.h> | |
159 | ||
8264deb8 G |
160 | static int udp_timeout_nlattr_to_obj(struct nlattr *tb[], |
161 | struct net *net, void *data) | |
50978462 PNA |
162 | { |
163 | unsigned int *timeouts = data; | |
8264deb8 | 164 | struct nf_udp_net *un = udp_pernet(net); |
50978462 PNA |
165 | |
166 | /* set default timeouts for UDP. */ | |
8264deb8 G |
167 | timeouts[UDP_CT_UNREPLIED] = un->timeouts[UDP_CT_UNREPLIED]; |
168 | timeouts[UDP_CT_REPLIED] = un->timeouts[UDP_CT_REPLIED]; | |
50978462 PNA |
169 | |
170 | if (tb[CTA_TIMEOUT_UDP_UNREPLIED]) { | |
171 | timeouts[UDP_CT_UNREPLIED] = | |
172 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_UNREPLIED])) * HZ; | |
173 | } | |
174 | if (tb[CTA_TIMEOUT_UDP_REPLIED]) { | |
175 | timeouts[UDP_CT_REPLIED] = | |
176 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_REPLIED])) * HZ; | |
177 | } | |
178 | return 0; | |
179 | } | |
180 | ||
181 | static int | |
182 | udp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data) | |
183 | { | |
184 | const unsigned int *timeouts = data; | |
185 | ||
3c60a17b DM |
186 | if (nla_put_be32(skb, CTA_TIMEOUT_UDP_UNREPLIED, |
187 | htonl(timeouts[UDP_CT_UNREPLIED] / HZ)) || | |
188 | nla_put_be32(skb, CTA_TIMEOUT_UDP_REPLIED, | |
189 | htonl(timeouts[UDP_CT_REPLIED] / HZ))) | |
190 | goto nla_put_failure; | |
50978462 PNA |
191 | return 0; |
192 | ||
193 | nla_put_failure: | |
194 | return -ENOSPC; | |
195 | } | |
196 | ||
197 | static const struct nla_policy | |
198 | udp_timeout_nla_policy[CTA_TIMEOUT_UDP_MAX+1] = { | |
199 | [CTA_TIMEOUT_UDP_UNREPLIED] = { .type = NLA_U32 }, | |
200 | [CTA_TIMEOUT_UDP_REPLIED] = { .type = NLA_U32 }, | |
201 | }; | |
202 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
203 | ||
933a41e7 | 204 | #ifdef CONFIG_SYSCTL |
933a41e7 PM |
205 | static struct ctl_table udp_sysctl_table[] = { |
206 | { | |
933a41e7 | 207 | .procname = "nf_conntrack_udp_timeout", |
933a41e7 PM |
208 | .maxlen = sizeof(unsigned int), |
209 | .mode = 0644, | |
6d9f239a | 210 | .proc_handler = proc_dointvec_jiffies, |
933a41e7 PM |
211 | }, |
212 | { | |
933a41e7 | 213 | .procname = "nf_conntrack_udp_timeout_stream", |
933a41e7 PM |
214 | .maxlen = sizeof(unsigned int), |
215 | .mode = 0644, | |
6d9f239a | 216 | .proc_handler = proc_dointvec_jiffies, |
933a41e7 | 217 | }, |
f8572d8f | 218 | { } |
933a41e7 | 219 | }; |
a999e683 PM |
220 | #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT |
221 | static struct ctl_table udp_compat_sysctl_table[] = { | |
222 | { | |
a999e683 | 223 | .procname = "ip_conntrack_udp_timeout", |
a999e683 PM |
224 | .maxlen = sizeof(unsigned int), |
225 | .mode = 0644, | |
6d9f239a | 226 | .proc_handler = proc_dointvec_jiffies, |
a999e683 PM |
227 | }, |
228 | { | |
a999e683 | 229 | .procname = "ip_conntrack_udp_timeout_stream", |
a999e683 PM |
230 | .maxlen = sizeof(unsigned int), |
231 | .mode = 0644, | |
6d9f239a | 232 | .proc_handler = proc_dointvec_jiffies, |
a999e683 | 233 | }, |
f8572d8f | 234 | { } |
a999e683 PM |
235 | }; |
236 | #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */ | |
933a41e7 PM |
237 | #endif /* CONFIG_SYSCTL */ |
238 | ||
dee7364e G |
239 | static int udp_kmemdup_sysctl_table(struct nf_proto_net *pn, |
240 | struct nf_udp_net *un) | |
0ce490ad G |
241 | { |
242 | #ifdef CONFIG_SYSCTL | |
0ce490ad G |
243 | if (pn->ctl_table) |
244 | return 0; | |
245 | pn->ctl_table = kmemdup(udp_sysctl_table, | |
246 | sizeof(udp_sysctl_table), | |
247 | GFP_KERNEL); | |
248 | if (!pn->ctl_table) | |
249 | return -ENOMEM; | |
250 | pn->ctl_table[0].data = &un->timeouts[UDP_CT_UNREPLIED]; | |
251 | pn->ctl_table[1].data = &un->timeouts[UDP_CT_REPLIED]; | |
252 | #endif | |
253 | return 0; | |
254 | } | |
255 | ||
dee7364e G |
256 | static int udp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn, |
257 | struct nf_udp_net *un) | |
0ce490ad G |
258 | { |
259 | #ifdef CONFIG_SYSCTL | |
260 | #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT | |
0ce490ad G |
261 | pn->ctl_compat_table = kmemdup(udp_compat_sysctl_table, |
262 | sizeof(udp_compat_sysctl_table), | |
263 | GFP_KERNEL); | |
264 | if (!pn->ctl_compat_table) | |
265 | return -ENOMEM; | |
266 | ||
267 | pn->ctl_compat_table[0].data = &un->timeouts[UDP_CT_UNREPLIED]; | |
268 | pn->ctl_compat_table[1].data = &un->timeouts[UDP_CT_REPLIED]; | |
269 | #endif | |
270 | #endif | |
271 | return 0; | |
272 | } | |
273 | ||
dee7364e | 274 | static int udp_init_net(struct net *net, u_int16_t proto) |
0ce490ad G |
275 | { |
276 | int ret; | |
277 | struct nf_udp_net *un = udp_pernet(net); | |
dee7364e | 278 | struct nf_proto_net *pn = &un->pn; |
0ce490ad | 279 | |
dee7364e G |
280 | if (!pn->users) { |
281 | int i; | |
0ce490ad | 282 | |
dee7364e G |
283 | for (i = 0; i < UDP_CT_MAX; i++) |
284 | un->timeouts[i] = udp_timeouts[i]; | |
0ce490ad | 285 | } |
0ce490ad | 286 | |
dee7364e G |
287 | if (proto == AF_INET) { |
288 | ret = udp_kmemdup_compat_sysctl_table(pn, un); | |
289 | if (ret < 0) | |
290 | return ret; | |
0ce490ad | 291 | |
dee7364e G |
292 | ret = udp_kmemdup_sysctl_table(pn, un); |
293 | if (ret < 0) | |
294 | nf_ct_kfree_compat_sysctl_table(pn); | |
295 | } else | |
296 | ret = udp_kmemdup_sysctl_table(pn, un); | |
297 | ||
298 | return ret; | |
0ce490ad G |
299 | } |
300 | ||
08911475 PNA |
301 | static struct nf_proto_net *udp_get_net_proto(struct net *net) |
302 | { | |
303 | return &net->ct.nf_ct_proto.udp.pn; | |
304 | } | |
305 | ||
61075af5 | 306 | struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly = |
9fb9cbb1 YK |
307 | { |
308 | .l3proto = PF_INET, | |
605dcad6 | 309 | .l4proto = IPPROTO_UDP, |
9fb9cbb1 YK |
310 | .name = "udp", |
311 | .pkt_to_tuple = udp_pkt_to_tuple, | |
312 | .invert_tuple = udp_invert_tuple, | |
313 | .print_tuple = udp_print_tuple, | |
9fb9cbb1 | 314 | .packet = udp_packet, |
2c8503f5 | 315 | .get_timeouts = udp_get_timeouts, |
9fb9cbb1 | 316 | .new = udp_new, |
96f6bf82 | 317 | .error = udp_error, |
c0cd1156 | 318 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 PM |
319 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
320 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, | |
a400c30e | 321 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
f73e924c | 322 | .nla_policy = nf_ct_port_nla_policy, |
c1d10adb | 323 | #endif |
50978462 PNA |
324 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
325 | .ctnl_timeout = { | |
326 | .nlattr_to_obj = udp_timeout_nlattr_to_obj, | |
327 | .obj_to_nlattr = udp_timeout_obj_to_nlattr, | |
328 | .nlattr_max = CTA_TIMEOUT_UDP_MAX, | |
329 | .obj_size = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX, | |
330 | .nla_policy = udp_timeout_nla_policy, | |
331 | }, | |
332 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
dee7364e | 333 | .init_net = udp_init_net, |
08911475 | 334 | .get_net_proto = udp_get_net_proto, |
9fb9cbb1 | 335 | }; |
13b18339 | 336 | EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4); |
9fb9cbb1 | 337 | |
61075af5 | 338 | struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly = |
9fb9cbb1 YK |
339 | { |
340 | .l3proto = PF_INET6, | |
605dcad6 | 341 | .l4proto = IPPROTO_UDP, |
9fb9cbb1 YK |
342 | .name = "udp", |
343 | .pkt_to_tuple = udp_pkt_to_tuple, | |
344 | .invert_tuple = udp_invert_tuple, | |
345 | .print_tuple = udp_print_tuple, | |
9fb9cbb1 | 346 | .packet = udp_packet, |
2c8503f5 | 347 | .get_timeouts = udp_get_timeouts, |
9fb9cbb1 | 348 | .new = udp_new, |
96f6bf82 | 349 | .error = udp_error, |
c0cd1156 | 350 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 PM |
351 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
352 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, | |
a400c30e | 353 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
f73e924c | 354 | .nla_policy = nf_ct_port_nla_policy, |
c1d10adb | 355 | #endif |
50978462 PNA |
356 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
357 | .ctnl_timeout = { | |
358 | .nlattr_to_obj = udp_timeout_nlattr_to_obj, | |
359 | .obj_to_nlattr = udp_timeout_obj_to_nlattr, | |
360 | .nlattr_max = CTA_TIMEOUT_UDP_MAX, | |
361 | .obj_size = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX, | |
362 | .nla_policy = udp_timeout_nla_policy, | |
363 | }, | |
364 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
dee7364e | 365 | .init_net = udp_init_net, |
08911475 | 366 | .get_net_proto = udp_get_net_proto, |
9fb9cbb1 | 367 | }; |
13b18339 | 368 | EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6); |