1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 * (C) 2007 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>
27 enum udplite_conntrack {
33 static unsigned int udplite_timeouts[UDPLITE_CT_MAX] = {
34 [UDPLITE_CT_UNREPLIED] = 30*HZ,
35 [UDPLITE_CT_REPLIED] = 180*HZ,
38 static int udplite_net_id __read_mostly;
40 struct nf_proto_net pn;
41 unsigned int timeouts[UDPLITE_CT_MAX];
44 static inline struct udplite_net *udplite_pernet(struct net *net)
46 return net_generic(net, udplite_net_id);
49 static bool udplite_pkt_to_tuple(const struct sk_buff *skb,
51 struct nf_conntrack_tuple *tuple)
53 const struct udphdr *hp;
56 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
60 tuple->src.u.udp.port = hp->source;
61 tuple->dst.u.udp.port = hp->dest;
65 static bool udplite_invert_tuple(struct nf_conntrack_tuple *tuple,
66 const struct nf_conntrack_tuple *orig)
68 tuple->src.u.udp.port = orig->dst.u.udp.port;
69 tuple->dst.u.udp.port = orig->src.u.udp.port;
73 /* Print out the per-protocol part of the tuple. */
74 static int udplite_print_tuple(struct seq_file *s,
75 const struct nf_conntrack_tuple *tuple)
77 return seq_printf(s, "sport=%hu dport=%hu ",
78 ntohs(tuple->src.u.udp.port),
79 ntohs(tuple->dst.u.udp.port));
82 static unsigned int *udplite_get_timeouts(struct net *net)
84 return udplite_pernet(net)->timeouts;
87 /* Returns verdict for packet, and may modify conntracktype */
88 static int udplite_packet(struct nf_conn *ct,
89 const struct sk_buff *skb,
91 enum ip_conntrack_info ctinfo,
94 unsigned int *timeouts)
96 /* If we've seen traffic both ways, this is some kind of UDP
97 stream. Extend timeout. */
98 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
99 nf_ct_refresh_acct(ct, ctinfo, skb,
100 timeouts[UDPLITE_CT_REPLIED]);
101 /* Also, more likely to be important, and not a probe */
102 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
103 nf_conntrack_event_cache(IPCT_ASSURED, ct);
105 nf_ct_refresh_acct(ct, ctinfo, skb,
106 timeouts[UDPLITE_CT_UNREPLIED]);
111 /* Called when a new connection for this protocol found. */
112 static bool udplite_new(struct nf_conn *ct, const struct sk_buff *skb,
113 unsigned int dataoff, unsigned int *timeouts)
118 static int udplite_error(struct net *net, struct nf_conn *tmpl,
120 unsigned int dataoff,
121 enum ip_conntrack_info *ctinfo,
123 unsigned int hooknum)
125 unsigned int udplen = skb->len - dataoff;
126 const struct udphdr *hdr;
130 /* Header is too small? */
131 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
133 if (LOG_INVALID(net, IPPROTO_UDPLITE))
134 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
135 "nf_ct_udplite: short packet ");
139 cscov = ntohs(hdr->len);
142 else if (cscov < sizeof(*hdr) || cscov > udplen) {
143 if (LOG_INVALID(net, IPPROTO_UDPLITE))
144 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
145 "nf_ct_udplite: invalid checksum coverage ");
149 /* UDPLITE mandates checksums */
151 if (LOG_INVALID(net, IPPROTO_UDPLITE))
152 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
153 "nf_ct_udplite: checksum missing ");
157 /* Checksum invalid? Ignore. */
158 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
159 nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_UDP,
161 if (LOG_INVALID(net, IPPROTO_UDPLITE))
162 nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
163 "nf_ct_udplite: bad UDPLite checksum ");
170 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
172 #include <linux/netfilter/nfnetlink.h>
173 #include <linux/netfilter/nfnetlink_cttimeout.h>
175 static int udplite_timeout_nlattr_to_obj(struct nlattr *tb[],
176 struct net *net, void *data)
178 unsigned int *timeouts = data;
179 struct udplite_net *un = udplite_pernet(net);
181 /* set default timeouts for UDPlite. */
182 timeouts[UDPLITE_CT_UNREPLIED] = un->timeouts[UDPLITE_CT_UNREPLIED];
183 timeouts[UDPLITE_CT_REPLIED] = un->timeouts[UDPLITE_CT_REPLIED];
185 if (tb[CTA_TIMEOUT_UDPLITE_UNREPLIED]) {
186 timeouts[UDPLITE_CT_UNREPLIED] =
187 ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_UNREPLIED])) * HZ;
189 if (tb[CTA_TIMEOUT_UDPLITE_REPLIED]) {
190 timeouts[UDPLITE_CT_REPLIED] =
191 ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_REPLIED])) * HZ;
197 udplite_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
199 const unsigned int *timeouts = data;
201 if (nla_put_be32(skb, CTA_TIMEOUT_UDPLITE_UNREPLIED,
202 htonl(timeouts[UDPLITE_CT_UNREPLIED] / HZ)) ||
203 nla_put_be32(skb, CTA_TIMEOUT_UDPLITE_REPLIED,
204 htonl(timeouts[UDPLITE_CT_REPLIED] / HZ)))
205 goto nla_put_failure;
212 static const struct nla_policy
213 udplite_timeout_nla_policy[CTA_TIMEOUT_UDPLITE_MAX+1] = {
214 [CTA_TIMEOUT_UDPLITE_UNREPLIED] = { .type = NLA_U32 },
215 [CTA_TIMEOUT_UDPLITE_REPLIED] = { .type = NLA_U32 },
217 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
220 static struct ctl_table udplite_sysctl_table[] = {
222 .procname = "nf_conntrack_udplite_timeout",
223 .maxlen = sizeof(unsigned int),
225 .proc_handler = proc_dointvec_jiffies,
228 .procname = "nf_conntrack_udplite_timeout_stream",
229 .maxlen = sizeof(unsigned int),
231 .proc_handler = proc_dointvec_jiffies,
235 #endif /* CONFIG_SYSCTL */
237 static int udplite_kmemdup_sysctl_table(struct nf_proto_net *pn,
238 struct udplite_net *un)
244 pn->ctl_table = kmemdup(udplite_sysctl_table,
245 sizeof(udplite_sysctl_table),
250 pn->ctl_table[0].data = &un->timeouts[UDPLITE_CT_UNREPLIED];
251 pn->ctl_table[1].data = &un->timeouts[UDPLITE_CT_REPLIED];
256 static int udplite_init_net(struct net *net, u_int16_t proto)
258 struct udplite_net *un = udplite_pernet(net);
259 struct nf_proto_net *pn = &un->pn;
264 for (i = 0 ; i < UDPLITE_CT_MAX; i++)
265 un->timeouts[i] = udplite_timeouts[i];
268 return udplite_kmemdup_sysctl_table(pn, un);
271 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly =
274 .l4proto = IPPROTO_UDPLITE,
276 .pkt_to_tuple = udplite_pkt_to_tuple,
277 .invert_tuple = udplite_invert_tuple,
278 .print_tuple = udplite_print_tuple,
279 .packet = udplite_packet,
280 .get_timeouts = udplite_get_timeouts,
282 .error = udplite_error,
283 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
284 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
285 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
286 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
287 .nla_policy = nf_ct_port_nla_policy,
289 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
291 .nlattr_to_obj = udplite_timeout_nlattr_to_obj,
292 .obj_to_nlattr = udplite_timeout_obj_to_nlattr,
293 .nlattr_max = CTA_TIMEOUT_UDPLITE_MAX,
294 .obj_size = sizeof(unsigned int) *
295 CTA_TIMEOUT_UDPLITE_MAX,
296 .nla_policy = udplite_timeout_nla_policy,
298 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
299 .net_id = &udplite_net_id,
300 .init_net = udplite_init_net,
303 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
306 .l4proto = IPPROTO_UDPLITE,
308 .pkt_to_tuple = udplite_pkt_to_tuple,
309 .invert_tuple = udplite_invert_tuple,
310 .print_tuple = udplite_print_tuple,
311 .packet = udplite_packet,
312 .get_timeouts = udplite_get_timeouts,
314 .error = udplite_error,
315 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
316 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
317 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
318 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
319 .nla_policy = nf_ct_port_nla_policy,
321 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
323 .nlattr_to_obj = udplite_timeout_nlattr_to_obj,
324 .obj_to_nlattr = udplite_timeout_obj_to_nlattr,
325 .nlattr_max = CTA_TIMEOUT_UDPLITE_MAX,
326 .obj_size = sizeof(unsigned int) *
327 CTA_TIMEOUT_UDPLITE_MAX,
328 .nla_policy = udplite_timeout_nla_policy,
330 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
331 .net_id = &udplite_net_id,
332 .init_net = udplite_init_net,
335 static int udplite_net_init(struct net *net)
339 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udplite4);
341 pr_err("nf_conntrack_udplite4: pernet registration failed.\n");
344 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udplite6);
346 pr_err("nf_conntrack_udplite6: pernet registration failed.\n");
347 goto cleanup_udplite4;
352 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite4);
357 static void udplite_net_exit(struct net *net)
359 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite6);
360 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udplite4);
363 static struct pernet_operations udplite_net_ops = {
364 .init = udplite_net_init,
365 .exit = udplite_net_exit,
366 .id = &udplite_net_id,
367 .size = sizeof(struct udplite_net),
370 static int __init nf_conntrack_proto_udplite_init(void)
374 ret = register_pernet_subsys(&udplite_net_ops);
378 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udplite4);
382 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udplite6);
388 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
390 unregister_pernet_subsys(&udplite_net_ops);
395 static void __exit nf_conntrack_proto_udplite_exit(void)
397 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite6);
398 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
399 unregister_pernet_subsys(&udplite_net_ops);
402 module_init(nf_conntrack_proto_udplite_init);
403 module_exit(nf_conntrack_proto_udplite_exit);
405 MODULE_LICENSE("GPL");