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/udp.h>
13 #include <linux/seq_file.h>
14 #include <linux/skbuff.h>
15 #include <linux/ipv6.h>
16 #include <net/ip6_checksum.h>
17 #include <net/checksum.h>
19 #include <linux/netfilter.h>
20 #include <linux/netfilter_ipv4.h>
21 #include <linux/netfilter_ipv6.h>
22 #include <net/netfilter/nf_conntrack_l4proto.h>
23 #include <net/netfilter/nf_conntrack_ecache.h>
24 #include <net/netfilter/nf_log.h>
26 static unsigned int udplite_timeouts
[UDPLITE_CT_MAX
] = {
27 [UDPLITE_CT_UNREPLIED
] = 30*HZ
,
28 [UDPLITE_CT_REPLIED
] = 180*HZ
,
31 static inline struct nf_udplite_net
*udplite_pernet(struct net
*net
)
33 return &net
->ct
.nf_ct_proto
.udplite
;
36 static bool udplite_pkt_to_tuple(const struct sk_buff
*skb
,
39 struct nf_conntrack_tuple
*tuple
)
41 const struct udphdr
*hp
;
44 /* Actually only need first 4 bytes to get ports. */
45 hp
= skb_header_pointer(skb
, dataoff
, 4, &_hdr
);
49 tuple
->src
.u
.udp
.port
= hp
->source
;
50 tuple
->dst
.u
.udp
.port
= hp
->dest
;
54 static bool udplite_invert_tuple(struct nf_conntrack_tuple
*tuple
,
55 const struct nf_conntrack_tuple
*orig
)
57 tuple
->src
.u
.udp
.port
= orig
->dst
.u
.udp
.port
;
58 tuple
->dst
.u
.udp
.port
= orig
->src
.u
.udp
.port
;
62 /* Print out the per-protocol part of the tuple. */
63 static void udplite_print_tuple(struct seq_file
*s
,
64 const struct nf_conntrack_tuple
*tuple
)
66 seq_printf(s
, "sport=%hu dport=%hu ",
67 ntohs(tuple
->src
.u
.udp
.port
),
68 ntohs(tuple
->dst
.u
.udp
.port
));
71 static unsigned int *udplite_get_timeouts(struct net
*net
)
73 return udplite_pernet(net
)->timeouts
;
76 /* Returns verdict for packet, and may modify conntracktype */
77 static int udplite_packet(struct nf_conn
*ct
,
78 const struct sk_buff
*skb
,
80 enum ip_conntrack_info ctinfo
,
83 unsigned int *timeouts
)
85 /* If we've seen traffic both ways, this is some kind of UDP
86 stream. Extend timeout. */
87 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)) {
88 nf_ct_refresh_acct(ct
, ctinfo
, skb
,
89 timeouts
[UDPLITE_CT_REPLIED
]);
90 /* Also, more likely to be important, and not a probe */
91 if (!test_and_set_bit(IPS_ASSURED_BIT
, &ct
->status
))
92 nf_conntrack_event_cache(IPCT_ASSURED
, ct
);
94 nf_ct_refresh_acct(ct
, ctinfo
, skb
,
95 timeouts
[UDPLITE_CT_UNREPLIED
]);
100 /* Called when a new connection for this protocol found. */
101 static bool udplite_new(struct nf_conn
*ct
, const struct sk_buff
*skb
,
102 unsigned int dataoff
, unsigned int *timeouts
)
107 static int udplite_error(struct net
*net
, struct nf_conn
*tmpl
,
109 unsigned int dataoff
,
110 enum ip_conntrack_info
*ctinfo
,
112 unsigned int hooknum
)
114 unsigned int udplen
= skb
->len
- dataoff
;
115 const struct udphdr
*hdr
;
119 /* Header is too small? */
120 hdr
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
122 if (LOG_INVALID(net
, IPPROTO_UDPLITE
))
123 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
124 "nf_ct_udplite: short packet ");
128 cscov
= ntohs(hdr
->len
);
131 else if (cscov
< sizeof(*hdr
) || cscov
> udplen
) {
132 if (LOG_INVALID(net
, IPPROTO_UDPLITE
))
133 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
134 "nf_ct_udplite: invalid checksum coverage ");
138 /* UDPLITE mandates checksums */
140 if (LOG_INVALID(net
, IPPROTO_UDPLITE
))
141 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
142 "nf_ct_udplite: checksum missing ");
146 /* Checksum invalid? Ignore. */
147 if (net
->ct
.sysctl_checksum
&& hooknum
== NF_INET_PRE_ROUTING
&&
148 nf_checksum_partial(skb
, hooknum
, dataoff
, cscov
, IPPROTO_UDP
,
150 if (LOG_INVALID(net
, IPPROTO_UDPLITE
))
151 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
152 "nf_ct_udplite: bad UDPLite checksum ");
159 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
161 #include <linux/netfilter/nfnetlink.h>
162 #include <linux/netfilter/nfnetlink_cttimeout.h>
164 static int udplite_timeout_nlattr_to_obj(struct nlattr
*tb
[],
165 struct net
*net
, void *data
)
167 unsigned int *timeouts
= data
;
168 struct nf_udplite_net
*un
= udplite_pernet(net
);
170 /* set default timeouts for UDPlite. */
171 timeouts
[UDPLITE_CT_UNREPLIED
] = un
->timeouts
[UDPLITE_CT_UNREPLIED
];
172 timeouts
[UDPLITE_CT_REPLIED
] = un
->timeouts
[UDPLITE_CT_REPLIED
];
174 if (tb
[CTA_TIMEOUT_UDPLITE_UNREPLIED
]) {
175 timeouts
[UDPLITE_CT_UNREPLIED
] =
176 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_UDPLITE_UNREPLIED
])) * HZ
;
178 if (tb
[CTA_TIMEOUT_UDPLITE_REPLIED
]) {
179 timeouts
[UDPLITE_CT_REPLIED
] =
180 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_UDPLITE_REPLIED
])) * HZ
;
186 udplite_timeout_obj_to_nlattr(struct sk_buff
*skb
, const void *data
)
188 const unsigned int *timeouts
= data
;
190 if (nla_put_be32(skb
, CTA_TIMEOUT_UDPLITE_UNREPLIED
,
191 htonl(timeouts
[UDPLITE_CT_UNREPLIED
] / HZ
)) ||
192 nla_put_be32(skb
, CTA_TIMEOUT_UDPLITE_REPLIED
,
193 htonl(timeouts
[UDPLITE_CT_REPLIED
] / HZ
)))
194 goto nla_put_failure
;
201 static const struct nla_policy
202 udplite_timeout_nla_policy
[CTA_TIMEOUT_UDPLITE_MAX
+1] = {
203 [CTA_TIMEOUT_UDPLITE_UNREPLIED
] = { .type
= NLA_U32
},
204 [CTA_TIMEOUT_UDPLITE_REPLIED
] = { .type
= NLA_U32
},
206 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
209 static struct ctl_table udplite_sysctl_table
[] = {
211 .procname
= "nf_conntrack_udplite_timeout",
212 .maxlen
= sizeof(unsigned int),
214 .proc_handler
= proc_dointvec_jiffies
,
217 .procname
= "nf_conntrack_udplite_timeout_stream",
218 .maxlen
= sizeof(unsigned int),
220 .proc_handler
= proc_dointvec_jiffies
,
224 #endif /* CONFIG_SYSCTL */
226 static int udplite_kmemdup_sysctl_table(struct nf_proto_net
*pn
,
227 struct nf_udplite_net
*un
)
233 pn
->ctl_table
= kmemdup(udplite_sysctl_table
,
234 sizeof(udplite_sysctl_table
),
239 pn
->ctl_table
[0].data
= &un
->timeouts
[UDPLITE_CT_UNREPLIED
];
240 pn
->ctl_table
[1].data
= &un
->timeouts
[UDPLITE_CT_REPLIED
];
245 static int udplite_init_net(struct net
*net
, u_int16_t proto
)
247 struct nf_udplite_net
*un
= udplite_pernet(net
);
248 struct nf_proto_net
*pn
= &un
->pn
;
253 for (i
= 0 ; i
< UDPLITE_CT_MAX
; i
++)
254 un
->timeouts
[i
] = udplite_timeouts
[i
];
257 return udplite_kmemdup_sysctl_table(pn
, un
);
260 struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly
=
263 .l4proto
= IPPROTO_UDPLITE
,
266 .pkt_to_tuple
= udplite_pkt_to_tuple
,
267 .invert_tuple
= udplite_invert_tuple
,
268 .print_tuple
= udplite_print_tuple
,
269 .packet
= udplite_packet
,
270 .get_timeouts
= udplite_get_timeouts
,
272 .error
= udplite_error
,
273 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
274 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
275 .nlattr_tuple_size
= nf_ct_port_nlattr_tuple_size
,
276 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
277 .nla_policy
= nf_ct_port_nla_policy
,
279 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
281 .nlattr_to_obj
= udplite_timeout_nlattr_to_obj
,
282 .obj_to_nlattr
= udplite_timeout_obj_to_nlattr
,
283 .nlattr_max
= CTA_TIMEOUT_UDPLITE_MAX
,
284 .obj_size
= sizeof(unsigned int) *
285 CTA_TIMEOUT_UDPLITE_MAX
,
286 .nla_policy
= udplite_timeout_nla_policy
,
288 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
289 .init_net
= udplite_init_net
,
291 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udplite4
);
293 struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly
=
296 .l4proto
= IPPROTO_UDPLITE
,
299 .pkt_to_tuple
= udplite_pkt_to_tuple
,
300 .invert_tuple
= udplite_invert_tuple
,
301 .print_tuple
= udplite_print_tuple
,
302 .packet
= udplite_packet
,
303 .get_timeouts
= udplite_get_timeouts
,
305 .error
= udplite_error
,
306 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
307 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
308 .nlattr_tuple_size
= nf_ct_port_nlattr_tuple_size
,
309 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
310 .nla_policy
= nf_ct_port_nla_policy
,
312 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
314 .nlattr_to_obj
= udplite_timeout_nlattr_to_obj
,
315 .obj_to_nlattr
= udplite_timeout_obj_to_nlattr
,
316 .nlattr_max
= CTA_TIMEOUT_UDPLITE_MAX
,
317 .obj_size
= sizeof(unsigned int) *
318 CTA_TIMEOUT_UDPLITE_MAX
,
319 .nla_policy
= udplite_timeout_nla_policy
,
321 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
322 .init_net
= udplite_init_net
,
324 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udplite6
);