1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 * (C) 2006-2012 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.
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_conntrack_timeout.h>
26 #include <net/netfilter/nf_log.h>
27 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
28 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
30 static const unsigned int udp_timeouts[UDP_CT_MAX] = {
31 [UDP_CT_UNREPLIED] = 30*HZ,
32 [UDP_CT_REPLIED] = 180*HZ,
35 static unsigned int *udp_get_timeouts(struct net *net)
37 return nf_udp_pernet(net)->timeouts;
40 static void udp_error_log(const struct sk_buff *skb,
41 const struct nf_hook_state *state,
44 nf_l4proto_log_invalid(skb, state->net, state->pf,
45 IPPROTO_UDP, "%s", msg);
48 static bool udp_error(struct sk_buff *skb,
50 const struct nf_hook_state *state)
52 unsigned int udplen = skb->len - dataoff;
53 const struct udphdr *hdr;
56 /* Header is too small? */
57 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
59 udp_error_log(skb, state, "short packet");
63 /* Truncated/malformed packets */
64 if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
65 udp_error_log(skb, state, "truncated/malformed packet");
69 /* Packet with no checksum */
73 /* Checksum invalid? Ignore.
74 * We skip checking packets on the outgoing path
75 * because the checksum is assumed to be correct.
76 * FIXME: Source route IP option packets --RR */
77 if (state->hook == NF_INET_PRE_ROUTING &&
78 state->net->ct.sysctl_checksum &&
79 nf_checksum(skb, state->hook, dataoff, IPPROTO_UDP, state->pf)) {
80 udp_error_log(skb, state, "bad checksum");
87 /* Returns verdict for packet, and may modify conntracktype */
88 static int udp_packet(struct nf_conn *ct,
91 enum ip_conntrack_info ctinfo,
92 const struct nf_hook_state *state)
94 unsigned int *timeouts;
96 if (udp_error(skb, dataoff, state))
99 timeouts = nf_ct_timeout_lookup(ct);
101 timeouts = udp_get_timeouts(nf_ct_net(ct));
103 /* If we've seen traffic both ways, this is some kind of UDP
104 stream. Extend timeout. */
105 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
106 nf_ct_refresh_acct(ct, ctinfo, skb,
107 timeouts[UDP_CT_REPLIED]);
108 /* Also, more likely to be important, and not a probe */
109 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
110 nf_conntrack_event_cache(IPCT_ASSURED, ct);
112 nf_ct_refresh_acct(ct, ctinfo, skb,
113 timeouts[UDP_CT_UNREPLIED]);
118 #ifdef CONFIG_NF_CT_PROTO_UDPLITE
119 static void udplite_error_log(const struct sk_buff *skb,
120 const struct nf_hook_state *state,
123 nf_l4proto_log_invalid(skb, state->net, state->pf,
124 IPPROTO_UDPLITE, "%s", msg);
127 static bool udplite_error(struct sk_buff *skb,
128 unsigned int dataoff,
129 const struct nf_hook_state *state)
131 unsigned int udplen = skb->len - dataoff;
132 const struct udphdr *hdr;
136 /* Header is too small? */
137 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
139 udplite_error_log(skb, state, "short packet");
143 cscov = ntohs(hdr->len);
146 } else if (cscov < sizeof(*hdr) || cscov > udplen) {
147 udplite_error_log(skb, state, "invalid checksum coverage");
151 /* UDPLITE mandates checksums */
153 udplite_error_log(skb, state, "checksum missing");
157 /* Checksum invalid? Ignore. */
158 if (state->hook == NF_INET_PRE_ROUTING &&
159 state->net->ct.sysctl_checksum &&
160 nf_checksum_partial(skb, state->hook, dataoff, cscov, IPPROTO_UDP,
162 udplite_error_log(skb, state, "bad checksum");
169 /* Returns verdict for packet, and may modify conntracktype */
170 static int udplite_packet(struct nf_conn *ct,
172 unsigned int dataoff,
173 enum ip_conntrack_info ctinfo,
174 const struct nf_hook_state *state)
176 unsigned int *timeouts;
178 if (udplite_error(skb, dataoff, state))
181 timeouts = nf_ct_timeout_lookup(ct);
183 timeouts = udp_get_timeouts(nf_ct_net(ct));
185 /* If we've seen traffic both ways, this is some kind of UDP
186 stream. Extend timeout. */
187 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
188 nf_ct_refresh_acct(ct, ctinfo, skb,
189 timeouts[UDP_CT_REPLIED]);
190 /* Also, more likely to be important, and not a probe */
191 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
192 nf_conntrack_event_cache(IPCT_ASSURED, ct);
194 nf_ct_refresh_acct(ct, ctinfo, skb,
195 timeouts[UDP_CT_UNREPLIED]);
201 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
203 #include <linux/netfilter/nfnetlink.h>
204 #include <linux/netfilter/nfnetlink_cttimeout.h>
206 static int udp_timeout_nlattr_to_obj(struct nlattr *tb[],
207 struct net *net, void *data)
209 unsigned int *timeouts = data;
210 struct nf_udp_net *un = nf_udp_pernet(net);
213 timeouts = un->timeouts;
215 /* set default timeouts for UDP. */
216 timeouts[UDP_CT_UNREPLIED] = un->timeouts[UDP_CT_UNREPLIED];
217 timeouts[UDP_CT_REPLIED] = un->timeouts[UDP_CT_REPLIED];
219 if (tb[CTA_TIMEOUT_UDP_UNREPLIED]) {
220 timeouts[UDP_CT_UNREPLIED] =
221 ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_UNREPLIED])) * HZ;
223 if (tb[CTA_TIMEOUT_UDP_REPLIED]) {
224 timeouts[UDP_CT_REPLIED] =
225 ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_REPLIED])) * HZ;
231 udp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
233 const unsigned int *timeouts = data;
235 if (nla_put_be32(skb, CTA_TIMEOUT_UDP_UNREPLIED,
236 htonl(timeouts[UDP_CT_UNREPLIED] / HZ)) ||
237 nla_put_be32(skb, CTA_TIMEOUT_UDP_REPLIED,
238 htonl(timeouts[UDP_CT_REPLIED] / HZ)))
239 goto nla_put_failure;
246 static const struct nla_policy
247 udp_timeout_nla_policy[CTA_TIMEOUT_UDP_MAX+1] = {
248 [CTA_TIMEOUT_UDP_UNREPLIED] = { .type = NLA_U32 },
249 [CTA_TIMEOUT_UDP_REPLIED] = { .type = NLA_U32 },
251 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
254 static struct ctl_table udp_sysctl_table[] = {
256 .procname = "nf_conntrack_udp_timeout",
257 .maxlen = sizeof(unsigned int),
259 .proc_handler = proc_dointvec_jiffies,
262 .procname = "nf_conntrack_udp_timeout_stream",
263 .maxlen = sizeof(unsigned int),
265 .proc_handler = proc_dointvec_jiffies,
269 #endif /* CONFIG_SYSCTL */
271 static int udp_kmemdup_sysctl_table(struct nf_proto_net *pn,
272 struct nf_udp_net *un)
277 pn->ctl_table = kmemdup(udp_sysctl_table,
278 sizeof(udp_sysctl_table),
282 pn->ctl_table[0].data = &un->timeouts[UDP_CT_UNREPLIED];
283 pn->ctl_table[1].data = &un->timeouts[UDP_CT_REPLIED];
288 static int udp_init_net(struct net *net)
290 struct nf_udp_net *un = nf_udp_pernet(net);
291 struct nf_proto_net *pn = &un->pn;
296 for (i = 0; i < UDP_CT_MAX; i++)
297 un->timeouts[i] = udp_timeouts[i];
300 return udp_kmemdup_sysctl_table(pn, un);
303 static struct nf_proto_net *udp_get_net_proto(struct net *net)
305 return &net->ct.nf_ct_proto.udp.pn;
308 const struct nf_conntrack_l4proto nf_conntrack_l4proto_udp =
310 .l4proto = IPPROTO_UDP,
312 .packet = udp_packet,
313 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
314 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
315 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
316 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
317 .nla_policy = nf_ct_port_nla_policy,
319 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
321 .nlattr_to_obj = udp_timeout_nlattr_to_obj,
322 .obj_to_nlattr = udp_timeout_obj_to_nlattr,
323 .nlattr_max = CTA_TIMEOUT_UDP_MAX,
324 .obj_size = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
325 .nla_policy = udp_timeout_nla_policy,
327 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
328 .init_net = udp_init_net,
329 .get_net_proto = udp_get_net_proto,
332 #ifdef CONFIG_NF_CT_PROTO_UDPLITE
333 const struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite =
335 .l4proto = IPPROTO_UDPLITE,
337 .packet = udplite_packet,
338 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
339 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
340 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
341 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
342 .nla_policy = nf_ct_port_nla_policy,
344 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
346 .nlattr_to_obj = udp_timeout_nlattr_to_obj,
347 .obj_to_nlattr = udp_timeout_obj_to_nlattr,
348 .nlattr_max = CTA_TIMEOUT_UDP_MAX,
349 .obj_size = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
350 .nla_policy = udp_timeout_nla_policy,
352 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
353 .init_net = udp_init_net,
354 .get_net_proto = udp_get_net_proto,