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 (!nf_ct_is_confirmed(ct))
104 ct->proto.udp.stream_ts = 2 * HZ + jiffies;
106 /* If we've seen traffic both ways, this is some kind of UDP
107 * stream. Set Assured.
109 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
110 unsigned long extra = timeouts[UDP_CT_UNREPLIED];
112 /* Still active after two seconds? Extend timeout. */
113 if (time_after(jiffies, ct->proto.udp.stream_ts))
114 extra = timeouts[UDP_CT_REPLIED];
116 nf_ct_refresh_acct(ct, ctinfo, skb, extra);
118 /* Also, more likely to be important, and not a probe */
119 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
120 nf_conntrack_event_cache(IPCT_ASSURED, ct);
122 nf_ct_refresh_acct(ct, ctinfo, skb,
123 timeouts[UDP_CT_UNREPLIED]);
128 #ifdef CONFIG_NF_CT_PROTO_UDPLITE
129 static void udplite_error_log(const struct sk_buff *skb,
130 const struct nf_hook_state *state,
133 nf_l4proto_log_invalid(skb, state->net, state->pf,
134 IPPROTO_UDPLITE, "%s", msg);
137 static bool udplite_error(struct sk_buff *skb,
138 unsigned int dataoff,
139 const struct nf_hook_state *state)
141 unsigned int udplen = skb->len - dataoff;
142 const struct udphdr *hdr;
146 /* Header is too small? */
147 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
149 udplite_error_log(skb, state, "short packet");
153 cscov = ntohs(hdr->len);
156 } else if (cscov < sizeof(*hdr) || cscov > udplen) {
157 udplite_error_log(skb, state, "invalid checksum coverage");
161 /* UDPLITE mandates checksums */
163 udplite_error_log(skb, state, "checksum missing");
167 /* Checksum invalid? Ignore. */
168 if (state->hook == NF_INET_PRE_ROUTING &&
169 state->net->ct.sysctl_checksum &&
170 nf_checksum_partial(skb, state->hook, dataoff, cscov, IPPROTO_UDP,
172 udplite_error_log(skb, state, "bad checksum");
179 /* Returns verdict for packet, and may modify conntracktype */
180 static int udplite_packet(struct nf_conn *ct,
182 unsigned int dataoff,
183 enum ip_conntrack_info ctinfo,
184 const struct nf_hook_state *state)
186 unsigned int *timeouts;
188 if (udplite_error(skb, dataoff, state))
191 timeouts = nf_ct_timeout_lookup(ct);
193 timeouts = udp_get_timeouts(nf_ct_net(ct));
195 /* If we've seen traffic both ways, this is some kind of UDP
196 stream. Extend timeout. */
197 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
198 nf_ct_refresh_acct(ct, ctinfo, skb,
199 timeouts[UDP_CT_REPLIED]);
200 /* Also, more likely to be important, and not a probe */
201 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
202 nf_conntrack_event_cache(IPCT_ASSURED, ct);
204 nf_ct_refresh_acct(ct, ctinfo, skb,
205 timeouts[UDP_CT_UNREPLIED]);
211 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
213 #include <linux/netfilter/nfnetlink.h>
214 #include <linux/netfilter/nfnetlink_cttimeout.h>
216 static int udp_timeout_nlattr_to_obj(struct nlattr *tb[],
217 struct net *net, void *data)
219 unsigned int *timeouts = data;
220 struct nf_udp_net *un = nf_udp_pernet(net);
223 timeouts = un->timeouts;
225 /* set default timeouts for UDP. */
226 timeouts[UDP_CT_UNREPLIED] = un->timeouts[UDP_CT_UNREPLIED];
227 timeouts[UDP_CT_REPLIED] = un->timeouts[UDP_CT_REPLIED];
229 if (tb[CTA_TIMEOUT_UDP_UNREPLIED]) {
230 timeouts[UDP_CT_UNREPLIED] =
231 ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_UNREPLIED])) * HZ;
233 if (tb[CTA_TIMEOUT_UDP_REPLIED]) {
234 timeouts[UDP_CT_REPLIED] =
235 ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_REPLIED])) * HZ;
241 udp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
243 const unsigned int *timeouts = data;
245 if (nla_put_be32(skb, CTA_TIMEOUT_UDP_UNREPLIED,
246 htonl(timeouts[UDP_CT_UNREPLIED] / HZ)) ||
247 nla_put_be32(skb, CTA_TIMEOUT_UDP_REPLIED,
248 htonl(timeouts[UDP_CT_REPLIED] / HZ)))
249 goto nla_put_failure;
256 static const struct nla_policy
257 udp_timeout_nla_policy[CTA_TIMEOUT_UDP_MAX+1] = {
258 [CTA_TIMEOUT_UDP_UNREPLIED] = { .type = NLA_U32 },
259 [CTA_TIMEOUT_UDP_REPLIED] = { .type = NLA_U32 },
261 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
264 static struct ctl_table udp_sysctl_table[] = {
266 .procname = "nf_conntrack_udp_timeout",
267 .maxlen = sizeof(unsigned int),
269 .proc_handler = proc_dointvec_jiffies,
272 .procname = "nf_conntrack_udp_timeout_stream",
273 .maxlen = sizeof(unsigned int),
275 .proc_handler = proc_dointvec_jiffies,
279 #endif /* CONFIG_SYSCTL */
281 static int udp_kmemdup_sysctl_table(struct nf_proto_net *pn,
282 struct nf_udp_net *un)
287 pn->ctl_table = kmemdup(udp_sysctl_table,
288 sizeof(udp_sysctl_table),
292 pn->ctl_table[0].data = &un->timeouts[UDP_CT_UNREPLIED];
293 pn->ctl_table[1].data = &un->timeouts[UDP_CT_REPLIED];
298 static int udp_init_net(struct net *net)
300 struct nf_udp_net *un = nf_udp_pernet(net);
301 struct nf_proto_net *pn = &un->pn;
306 for (i = 0; i < UDP_CT_MAX; i++)
307 un->timeouts[i] = udp_timeouts[i];
310 return udp_kmemdup_sysctl_table(pn, un);
313 static struct nf_proto_net *udp_get_net_proto(struct net *net)
315 return &net->ct.nf_ct_proto.udp.pn;
318 const struct nf_conntrack_l4proto nf_conntrack_l4proto_udp =
320 .l4proto = IPPROTO_UDP,
322 .packet = udp_packet,
323 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
324 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
325 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
326 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
327 .nla_policy = nf_ct_port_nla_policy,
329 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
331 .nlattr_to_obj = udp_timeout_nlattr_to_obj,
332 .obj_to_nlattr = udp_timeout_obj_to_nlattr,
333 .nlattr_max = CTA_TIMEOUT_UDP_MAX,
334 .obj_size = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
335 .nla_policy = udp_timeout_nla_policy,
337 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
338 .init_net = udp_init_net,
339 .get_net_proto = udp_get_net_proto,
342 #ifdef CONFIG_NF_CT_PROTO_UDPLITE
343 const struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite =
345 .l4proto = IPPROTO_UDPLITE,
347 .packet = udplite_packet,
348 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
349 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
350 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
351 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
352 .nla_policy = nf_ct_port_nla_policy,
354 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
356 .nlattr_to_obj = udp_timeout_nlattr_to_obj,
357 .obj_to_nlattr = udp_timeout_obj_to_nlattr,
358 .nlattr_max = CTA_TIMEOUT_UDP_MAX,
359 .obj_size = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
360 .nla_policy = udp_timeout_nla_policy,
362 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
363 .init_net = udp_init_net,
364 .get_net_proto = udp_get_net_proto,