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_log.h>
26 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
27 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
29 static unsigned int udp_timeouts
[UDP_CT_MAX
] = {
30 [UDP_CT_UNREPLIED
] = 30*HZ
,
31 [UDP_CT_REPLIED
] = 180*HZ
,
34 static inline struct nf_udp_net
*udp_pernet(struct net
*net
)
36 return &net
->ct
.nf_ct_proto
.udp
;
39 static bool udp_pkt_to_tuple(const struct sk_buff
*skb
,
42 struct nf_conntrack_tuple
*tuple
)
44 const struct udphdr
*hp
;
47 /* Actually only need first 4 bytes to get ports. */
48 hp
= skb_header_pointer(skb
, dataoff
, 4, &_hdr
);
52 tuple
->src
.u
.udp
.port
= hp
->source
;
53 tuple
->dst
.u
.udp
.port
= hp
->dest
;
58 static bool udp_invert_tuple(struct nf_conntrack_tuple
*tuple
,
59 const struct nf_conntrack_tuple
*orig
)
61 tuple
->src
.u
.udp
.port
= orig
->dst
.u
.udp
.port
;
62 tuple
->dst
.u
.udp
.port
= orig
->src
.u
.udp
.port
;
66 static unsigned int *udp_get_timeouts(struct net
*net
)
68 return udp_pernet(net
)->timeouts
;
71 /* Returns verdict for packet, and may modify conntracktype */
72 static int udp_packet(struct nf_conn
*ct
,
73 const struct sk_buff
*skb
,
75 enum ip_conntrack_info ctinfo
,
77 unsigned int *timeouts
)
79 /* If we've seen traffic both ways, this is some kind of UDP
80 stream. Extend timeout. */
81 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)) {
82 nf_ct_refresh_acct(ct
, ctinfo
, skb
,
83 timeouts
[UDP_CT_REPLIED
]);
84 /* Also, more likely to be important, and not a probe */
85 if (!test_and_set_bit(IPS_ASSURED_BIT
, &ct
->status
))
86 nf_conntrack_event_cache(IPCT_ASSURED
, ct
);
88 nf_ct_refresh_acct(ct
, ctinfo
, skb
,
89 timeouts
[UDP_CT_UNREPLIED
]);
94 /* Called when a new connection for this protocol found. */
95 static bool udp_new(struct nf_conn
*ct
, const struct sk_buff
*skb
,
96 unsigned int dataoff
, unsigned int *timeouts
)
101 #ifdef CONFIG_NF_CT_PROTO_UDPLITE
102 static int udplite_error(struct net
*net
, struct nf_conn
*tmpl
,
104 unsigned int dataoff
,
105 u8 pf
, unsigned int hooknum
)
107 unsigned int udplen
= skb
->len
- dataoff
;
108 const struct udphdr
*hdr
;
112 /* Header is too small? */
113 hdr
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
115 if (LOG_INVALID(net
, IPPROTO_UDPLITE
))
116 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
117 "nf_ct_udplite: short packet ");
121 cscov
= ntohs(hdr
->len
);
124 } else if (cscov
< sizeof(*hdr
) || cscov
> udplen
) {
125 if (LOG_INVALID(net
, IPPROTO_UDPLITE
))
126 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
127 "nf_ct_udplite: invalid checksum coverage ");
131 /* UDPLITE mandates checksums */
133 if (LOG_INVALID(net
, IPPROTO_UDPLITE
))
134 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
135 "nf_ct_udplite: checksum missing ");
139 /* Checksum invalid? Ignore. */
140 if (net
->ct
.sysctl_checksum
&& hooknum
== NF_INET_PRE_ROUTING
&&
141 nf_checksum_partial(skb
, hooknum
, dataoff
, cscov
, IPPROTO_UDP
,
143 if (LOG_INVALID(net
, IPPROTO_UDPLITE
))
144 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
145 "nf_ct_udplite: bad UDPLite checksum ");
153 static int udp_error(struct net
*net
, struct nf_conn
*tmpl
, struct sk_buff
*skb
,
154 unsigned int dataoff
,
156 unsigned int hooknum
)
158 unsigned int udplen
= skb
->len
- dataoff
;
159 const struct udphdr
*hdr
;
162 /* Header is too small? */
163 hdr
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
165 if (LOG_INVALID(net
, IPPROTO_UDP
))
166 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
167 "nf_ct_udp: short packet ");
171 /* Truncated/malformed packets */
172 if (ntohs(hdr
->len
) > udplen
|| ntohs(hdr
->len
) < sizeof(*hdr
)) {
173 if (LOG_INVALID(net
, IPPROTO_UDP
))
174 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
175 "nf_ct_udp: truncated/malformed packet ");
179 /* Packet with no checksum */
183 /* Checksum invalid? Ignore.
184 * We skip checking packets on the outgoing path
185 * because the checksum is assumed to be correct.
186 * FIXME: Source route IP option packets --RR */
187 if (net
->ct
.sysctl_checksum
&& hooknum
== NF_INET_PRE_ROUTING
&&
188 nf_checksum(skb
, hooknum
, dataoff
, IPPROTO_UDP
, pf
)) {
189 if (LOG_INVALID(net
, IPPROTO_UDP
))
190 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
191 "nf_ct_udp: bad UDP checksum ");
198 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
200 #include <linux/netfilter/nfnetlink.h>
201 #include <linux/netfilter/nfnetlink_cttimeout.h>
203 static int udp_timeout_nlattr_to_obj(struct nlattr
*tb
[],
204 struct net
*net
, void *data
)
206 unsigned int *timeouts
= data
;
207 struct nf_udp_net
*un
= udp_pernet(net
);
209 /* set default timeouts for UDP. */
210 timeouts
[UDP_CT_UNREPLIED
] = un
->timeouts
[UDP_CT_UNREPLIED
];
211 timeouts
[UDP_CT_REPLIED
] = un
->timeouts
[UDP_CT_REPLIED
];
213 if (tb
[CTA_TIMEOUT_UDP_UNREPLIED
]) {
214 timeouts
[UDP_CT_UNREPLIED
] =
215 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_UDP_UNREPLIED
])) * HZ
;
217 if (tb
[CTA_TIMEOUT_UDP_REPLIED
]) {
218 timeouts
[UDP_CT_REPLIED
] =
219 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_UDP_REPLIED
])) * HZ
;
225 udp_timeout_obj_to_nlattr(struct sk_buff
*skb
, const void *data
)
227 const unsigned int *timeouts
= data
;
229 if (nla_put_be32(skb
, CTA_TIMEOUT_UDP_UNREPLIED
,
230 htonl(timeouts
[UDP_CT_UNREPLIED
] / HZ
)) ||
231 nla_put_be32(skb
, CTA_TIMEOUT_UDP_REPLIED
,
232 htonl(timeouts
[UDP_CT_REPLIED
] / HZ
)))
233 goto nla_put_failure
;
240 static const struct nla_policy
241 udp_timeout_nla_policy
[CTA_TIMEOUT_UDP_MAX
+1] = {
242 [CTA_TIMEOUT_UDP_UNREPLIED
] = { .type
= NLA_U32
},
243 [CTA_TIMEOUT_UDP_REPLIED
] = { .type
= NLA_U32
},
245 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
248 static struct ctl_table udp_sysctl_table
[] = {
250 .procname
= "nf_conntrack_udp_timeout",
251 .maxlen
= sizeof(unsigned int),
253 .proc_handler
= proc_dointvec_jiffies
,
256 .procname
= "nf_conntrack_udp_timeout_stream",
257 .maxlen
= sizeof(unsigned int),
259 .proc_handler
= proc_dointvec_jiffies
,
263 #endif /* CONFIG_SYSCTL */
265 static int udp_kmemdup_sysctl_table(struct nf_proto_net
*pn
,
266 struct nf_udp_net
*un
)
271 pn
->ctl_table
= kmemdup(udp_sysctl_table
,
272 sizeof(udp_sysctl_table
),
276 pn
->ctl_table
[0].data
= &un
->timeouts
[UDP_CT_UNREPLIED
];
277 pn
->ctl_table
[1].data
= &un
->timeouts
[UDP_CT_REPLIED
];
282 static int udp_init_net(struct net
*net
, u_int16_t proto
)
284 struct nf_udp_net
*un
= udp_pernet(net
);
285 struct nf_proto_net
*pn
= &un
->pn
;
290 for (i
= 0; i
< UDP_CT_MAX
; i
++)
291 un
->timeouts
[i
] = udp_timeouts
[i
];
294 return udp_kmemdup_sysctl_table(pn
, un
);
297 static struct nf_proto_net
*udp_get_net_proto(struct net
*net
)
299 return &net
->ct
.nf_ct_proto
.udp
.pn
;
302 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly
=
305 .l4proto
= IPPROTO_UDP
,
307 .pkt_to_tuple
= udp_pkt_to_tuple
,
308 .invert_tuple
= udp_invert_tuple
,
309 .packet
= udp_packet
,
310 .get_timeouts
= udp_get_timeouts
,
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 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_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_CT_NETLINK_TIMEOUT */
328 .init_net
= udp_init_net
,
329 .get_net_proto
= udp_get_net_proto
,
331 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4
);
333 #ifdef CONFIG_NF_CT_PROTO_UDPLITE
334 struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly
=
337 .l4proto
= IPPROTO_UDPLITE
,
339 .pkt_to_tuple
= udp_pkt_to_tuple
,
340 .invert_tuple
= udp_invert_tuple
,
341 .packet
= udp_packet
,
342 .get_timeouts
= udp_get_timeouts
,
344 .error
= udplite_error
,
345 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
346 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
347 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
348 .nlattr_tuple_size
= nf_ct_port_nlattr_tuple_size
,
349 .nla_policy
= nf_ct_port_nla_policy
,
351 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
353 .nlattr_to_obj
= udp_timeout_nlattr_to_obj
,
354 .obj_to_nlattr
= udp_timeout_obj_to_nlattr
,
355 .nlattr_max
= CTA_TIMEOUT_UDP_MAX
,
356 .obj_size
= sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX
,
357 .nla_policy
= udp_timeout_nla_policy
,
359 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
360 .init_net
= udp_init_net
,
361 .get_net_proto
= udp_get_net_proto
,
363 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udplite4
);
366 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly
=
369 .l4proto
= IPPROTO_UDP
,
371 .pkt_to_tuple
= udp_pkt_to_tuple
,
372 .invert_tuple
= udp_invert_tuple
,
373 .packet
= udp_packet
,
374 .get_timeouts
= udp_get_timeouts
,
377 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
378 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
379 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
380 .nlattr_tuple_size
= nf_ct_port_nlattr_tuple_size
,
381 .nla_policy
= nf_ct_port_nla_policy
,
383 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
385 .nlattr_to_obj
= udp_timeout_nlattr_to_obj
,
386 .obj_to_nlattr
= udp_timeout_obj_to_nlattr
,
387 .nlattr_max
= CTA_TIMEOUT_UDP_MAX
,
388 .obj_size
= sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX
,
389 .nla_policy
= udp_timeout_nla_policy
,
391 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
392 .init_net
= udp_init_net
,
393 .get_net_proto
= udp_get_net_proto
,
395 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6
);
397 #ifdef CONFIG_NF_CT_PROTO_UDPLITE
398 struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly
=
401 .l4proto
= IPPROTO_UDPLITE
,
403 .pkt_to_tuple
= udp_pkt_to_tuple
,
404 .invert_tuple
= udp_invert_tuple
,
405 .packet
= udp_packet
,
406 .get_timeouts
= udp_get_timeouts
,
408 .error
= udplite_error
,
409 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
410 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
411 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
412 .nlattr_tuple_size
= nf_ct_port_nlattr_tuple_size
,
413 .nla_policy
= nf_ct_port_nla_policy
,
415 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
417 .nlattr_to_obj
= udp_timeout_nlattr_to_obj
,
418 .obj_to_nlattr
= udp_timeout_obj_to_nlattr
,
419 .nlattr_max
= CTA_TIMEOUT_UDP_MAX
,
420 .obj_size
= sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX
,
421 .nla_policy
= udp_timeout_nla_policy
,
423 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
424 .init_net
= udp_init_net
,
425 .get_net_proto
= udp_get_net_proto
,
427 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udplite6
);