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
,
52 struct nf_conntrack_tuple
*tuple
)
54 const struct udphdr
*hp
;
57 /* Actually only need first 4 bytes to get ports. */
58 hp
= skb_header_pointer(skb
, dataoff
, 4, &_hdr
);
62 tuple
->src
.u
.udp
.port
= hp
->source
;
63 tuple
->dst
.u
.udp
.port
= hp
->dest
;
67 static bool udplite_invert_tuple(struct nf_conntrack_tuple
*tuple
,
68 const struct nf_conntrack_tuple
*orig
)
70 tuple
->src
.u
.udp
.port
= orig
->dst
.u
.udp
.port
;
71 tuple
->dst
.u
.udp
.port
= orig
->src
.u
.udp
.port
;
75 /* Print out the per-protocol part of the tuple. */
76 static void udplite_print_tuple(struct seq_file
*s
,
77 const struct nf_conntrack_tuple
*tuple
)
79 seq_printf(s
, "sport=%hu dport=%hu ",
80 ntohs(tuple
->src
.u
.udp
.port
),
81 ntohs(tuple
->dst
.u
.udp
.port
));
84 static unsigned int *udplite_get_timeouts(struct net
*net
)
86 return udplite_pernet(net
)->timeouts
;
89 /* Returns verdict for packet, and may modify conntracktype */
90 static int udplite_packet(struct nf_conn
*ct
,
91 const struct sk_buff
*skb
,
93 enum ip_conntrack_info ctinfo
,
96 unsigned int *timeouts
)
98 /* If we've seen traffic both ways, this is some kind of UDP
99 stream. Extend timeout. */
100 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)) {
101 nf_ct_refresh_acct(ct
, ctinfo
, skb
,
102 timeouts
[UDPLITE_CT_REPLIED
]);
103 /* Also, more likely to be important, and not a probe */
104 if (!test_and_set_bit(IPS_ASSURED_BIT
, &ct
->status
))
105 nf_conntrack_event_cache(IPCT_ASSURED
, ct
);
107 nf_ct_refresh_acct(ct
, ctinfo
, skb
,
108 timeouts
[UDPLITE_CT_UNREPLIED
]);
113 /* Called when a new connection for this protocol found. */
114 static bool udplite_new(struct nf_conn
*ct
, const struct sk_buff
*skb
,
115 unsigned int dataoff
, unsigned int *timeouts
)
120 static int udplite_error(struct net
*net
, struct nf_conn
*tmpl
,
122 unsigned int dataoff
,
123 enum ip_conntrack_info
*ctinfo
,
125 unsigned int hooknum
)
127 unsigned int udplen
= skb
->len
- dataoff
;
128 const struct udphdr
*hdr
;
132 /* Header is too small? */
133 hdr
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
135 if (LOG_INVALID(net
, IPPROTO_UDPLITE
))
136 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
137 "nf_ct_udplite: short packet ");
141 cscov
= ntohs(hdr
->len
);
144 else if (cscov
< sizeof(*hdr
) || cscov
> udplen
) {
145 if (LOG_INVALID(net
, IPPROTO_UDPLITE
))
146 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
147 "nf_ct_udplite: invalid checksum coverage ");
151 /* UDPLITE mandates checksums */
153 if (LOG_INVALID(net
, IPPROTO_UDPLITE
))
154 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
155 "nf_ct_udplite: checksum missing ");
159 /* Checksum invalid? Ignore. */
160 if (net
->ct
.sysctl_checksum
&& hooknum
== NF_INET_PRE_ROUTING
&&
161 nf_checksum_partial(skb
, hooknum
, dataoff
, cscov
, IPPROTO_UDP
,
163 if (LOG_INVALID(net
, IPPROTO_UDPLITE
))
164 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
165 "nf_ct_udplite: bad UDPLite checksum ");
172 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
174 #include <linux/netfilter/nfnetlink.h>
175 #include <linux/netfilter/nfnetlink_cttimeout.h>
177 static int udplite_timeout_nlattr_to_obj(struct nlattr
*tb
[],
178 struct net
*net
, void *data
)
180 unsigned int *timeouts
= data
;
181 struct udplite_net
*un
= udplite_pernet(net
);
183 /* set default timeouts for UDPlite. */
184 timeouts
[UDPLITE_CT_UNREPLIED
] = un
->timeouts
[UDPLITE_CT_UNREPLIED
];
185 timeouts
[UDPLITE_CT_REPLIED
] = un
->timeouts
[UDPLITE_CT_REPLIED
];
187 if (tb
[CTA_TIMEOUT_UDPLITE_UNREPLIED
]) {
188 timeouts
[UDPLITE_CT_UNREPLIED
] =
189 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_UDPLITE_UNREPLIED
])) * HZ
;
191 if (tb
[CTA_TIMEOUT_UDPLITE_REPLIED
]) {
192 timeouts
[UDPLITE_CT_REPLIED
] =
193 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_UDPLITE_REPLIED
])) * HZ
;
199 udplite_timeout_obj_to_nlattr(struct sk_buff
*skb
, const void *data
)
201 const unsigned int *timeouts
= data
;
203 if (nla_put_be32(skb
, CTA_TIMEOUT_UDPLITE_UNREPLIED
,
204 htonl(timeouts
[UDPLITE_CT_UNREPLIED
] / HZ
)) ||
205 nla_put_be32(skb
, CTA_TIMEOUT_UDPLITE_REPLIED
,
206 htonl(timeouts
[UDPLITE_CT_REPLIED
] / HZ
)))
207 goto nla_put_failure
;
214 static const struct nla_policy
215 udplite_timeout_nla_policy
[CTA_TIMEOUT_UDPLITE_MAX
+1] = {
216 [CTA_TIMEOUT_UDPLITE_UNREPLIED
] = { .type
= NLA_U32
},
217 [CTA_TIMEOUT_UDPLITE_REPLIED
] = { .type
= NLA_U32
},
219 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
222 static struct ctl_table udplite_sysctl_table
[] = {
224 .procname
= "nf_conntrack_udplite_timeout",
225 .maxlen
= sizeof(unsigned int),
227 .proc_handler
= proc_dointvec_jiffies
,
230 .procname
= "nf_conntrack_udplite_timeout_stream",
231 .maxlen
= sizeof(unsigned int),
233 .proc_handler
= proc_dointvec_jiffies
,
237 #endif /* CONFIG_SYSCTL */
239 static int udplite_kmemdup_sysctl_table(struct nf_proto_net
*pn
,
240 struct udplite_net
*un
)
246 pn
->ctl_table
= kmemdup(udplite_sysctl_table
,
247 sizeof(udplite_sysctl_table
),
252 pn
->ctl_table
[0].data
= &un
->timeouts
[UDPLITE_CT_UNREPLIED
];
253 pn
->ctl_table
[1].data
= &un
->timeouts
[UDPLITE_CT_REPLIED
];
258 static int udplite_init_net(struct net
*net
, u_int16_t proto
)
260 struct udplite_net
*un
= udplite_pernet(net
);
261 struct nf_proto_net
*pn
= &un
->pn
;
266 for (i
= 0 ; i
< UDPLITE_CT_MAX
; i
++)
267 un
->timeouts
[i
] = udplite_timeouts
[i
];
270 return udplite_kmemdup_sysctl_table(pn
, un
);
273 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly
=
276 .l4proto
= IPPROTO_UDPLITE
,
279 .pkt_to_tuple
= udplite_pkt_to_tuple
,
280 .invert_tuple
= udplite_invert_tuple
,
281 .print_tuple
= udplite_print_tuple
,
282 .packet
= udplite_packet
,
283 .get_timeouts
= udplite_get_timeouts
,
285 .error
= udplite_error
,
286 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
287 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
288 .nlattr_tuple_size
= nf_ct_port_nlattr_tuple_size
,
289 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
290 .nla_policy
= nf_ct_port_nla_policy
,
292 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
294 .nlattr_to_obj
= udplite_timeout_nlattr_to_obj
,
295 .obj_to_nlattr
= udplite_timeout_obj_to_nlattr
,
296 .nlattr_max
= CTA_TIMEOUT_UDPLITE_MAX
,
297 .obj_size
= sizeof(unsigned int) *
298 CTA_TIMEOUT_UDPLITE_MAX
,
299 .nla_policy
= udplite_timeout_nla_policy
,
301 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
302 .net_id
= &udplite_net_id
,
303 .init_net
= udplite_init_net
,
306 static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly
=
309 .l4proto
= IPPROTO_UDPLITE
,
312 .pkt_to_tuple
= udplite_pkt_to_tuple
,
313 .invert_tuple
= udplite_invert_tuple
,
314 .print_tuple
= udplite_print_tuple
,
315 .packet
= udplite_packet
,
316 .get_timeouts
= udplite_get_timeouts
,
318 .error
= udplite_error
,
319 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
320 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
321 .nlattr_tuple_size
= nf_ct_port_nlattr_tuple_size
,
322 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
323 .nla_policy
= nf_ct_port_nla_policy
,
325 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
327 .nlattr_to_obj
= udplite_timeout_nlattr_to_obj
,
328 .obj_to_nlattr
= udplite_timeout_obj_to_nlattr
,
329 .nlattr_max
= CTA_TIMEOUT_UDPLITE_MAX
,
330 .obj_size
= sizeof(unsigned int) *
331 CTA_TIMEOUT_UDPLITE_MAX
,
332 .nla_policy
= udplite_timeout_nla_policy
,
334 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
335 .net_id
= &udplite_net_id
,
336 .init_net
= udplite_init_net
,
339 static int udplite_net_init(struct net
*net
)
343 ret
= nf_ct_l4proto_pernet_register(net
, &nf_conntrack_l4proto_udplite4
);
345 pr_err("nf_conntrack_udplite4: pernet registration failed.\n");
348 ret
= nf_ct_l4proto_pernet_register(net
, &nf_conntrack_l4proto_udplite6
);
350 pr_err("nf_conntrack_udplite6: pernet registration failed.\n");
351 goto cleanup_udplite4
;
356 nf_ct_l4proto_pernet_unregister(net
, &nf_conntrack_l4proto_udplite4
);
361 static void udplite_net_exit(struct net
*net
)
363 nf_ct_l4proto_pernet_unregister(net
, &nf_conntrack_l4proto_udplite6
);
364 nf_ct_l4proto_pernet_unregister(net
, &nf_conntrack_l4proto_udplite4
);
367 static struct pernet_operations udplite_net_ops
= {
368 .init
= udplite_net_init
,
369 .exit
= udplite_net_exit
,
370 .id
= &udplite_net_id
,
371 .size
= sizeof(struct udplite_net
),
374 static int __init
nf_conntrack_proto_udplite_init(void)
378 ret
= register_pernet_subsys(&udplite_net_ops
);
382 ret
= nf_ct_l4proto_register(&nf_conntrack_l4proto_udplite4
);
386 ret
= nf_ct_l4proto_register(&nf_conntrack_l4proto_udplite6
);
392 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite4
);
394 unregister_pernet_subsys(&udplite_net_ops
);
399 static void __exit
nf_conntrack_proto_udplite_exit(void)
401 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite6
);
402 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udplite4
);
403 unregister_pernet_subsys(&udplite_net_ops
);
406 module_init(nf_conntrack_proto_udplite_init
);
407 module_exit(nf_conntrack_proto_udplite_exit
);
409 MODULE_LICENSE("GPL");