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