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 /* Print out the per-protocol part of the tuple. */
67 static void udp_print_tuple(struct seq_file
*s
,
68 const struct nf_conntrack_tuple
*tuple
)
70 seq_printf(s
, "sport=%hu dport=%hu ",
71 ntohs(tuple
->src
.u
.udp
.port
),
72 ntohs(tuple
->dst
.u
.udp
.port
));
75 static unsigned int *udp_get_timeouts(struct net
*net
)
77 return udp_pernet(net
)->timeouts
;
80 /* Returns verdict for packet, and may modify conntracktype */
81 static int udp_packet(struct nf_conn
*ct
,
82 const struct sk_buff
*skb
,
84 enum ip_conntrack_info ctinfo
,
87 unsigned int *timeouts
)
89 /* If we've seen traffic both ways, this is some kind of UDP
90 stream. Extend timeout. */
91 if (test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)) {
92 nf_ct_refresh_acct(ct
, ctinfo
, skb
,
93 timeouts
[UDP_CT_REPLIED
]);
94 /* Also, more likely to be important, and not a probe */
95 if (!test_and_set_bit(IPS_ASSURED_BIT
, &ct
->status
))
96 nf_conntrack_event_cache(IPCT_ASSURED
, ct
);
98 nf_ct_refresh_acct(ct
, ctinfo
, skb
,
99 timeouts
[UDP_CT_UNREPLIED
]);
104 /* Called when a new connection for this protocol found. */
105 static bool udp_new(struct nf_conn
*ct
, const struct sk_buff
*skb
,
106 unsigned int dataoff
, unsigned int *timeouts
)
111 static int udp_error(struct net
*net
, struct nf_conn
*tmpl
, struct sk_buff
*skb
,
112 unsigned int dataoff
, enum ip_conntrack_info
*ctinfo
,
114 unsigned int hooknum
)
116 unsigned int udplen
= skb
->len
- dataoff
;
117 const struct udphdr
*hdr
;
120 /* Header is too small? */
121 hdr
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
123 if (LOG_INVALID(net
, IPPROTO_UDP
))
124 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
125 "nf_ct_udp: short packet ");
129 /* Truncated/malformed packets */
130 if (ntohs(hdr
->len
) > udplen
|| ntohs(hdr
->len
) < sizeof(*hdr
)) {
131 if (LOG_INVALID(net
, IPPROTO_UDP
))
132 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
133 "nf_ct_udp: truncated/malformed packet ");
137 /* Packet with no checksum */
141 /* Checksum invalid? Ignore.
142 * We skip checking packets on the outgoing path
143 * because the checksum is assumed to be correct.
144 * FIXME: Source route IP option packets --RR */
145 if (net
->ct
.sysctl_checksum
&& hooknum
== NF_INET_PRE_ROUTING
&&
146 nf_checksum(skb
, hooknum
, dataoff
, IPPROTO_UDP
, pf
)) {
147 if (LOG_INVALID(net
, IPPROTO_UDP
))
148 nf_log_packet(net
, pf
, 0, skb
, NULL
, NULL
, NULL
,
149 "nf_ct_udp: bad UDP checksum ");
156 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
158 #include <linux/netfilter/nfnetlink.h>
159 #include <linux/netfilter/nfnetlink_cttimeout.h>
161 static int udp_timeout_nlattr_to_obj(struct nlattr
*tb
[],
162 struct net
*net
, void *data
)
164 unsigned int *timeouts
= data
;
165 struct nf_udp_net
*un
= udp_pernet(net
);
167 /* set default timeouts for UDP. */
168 timeouts
[UDP_CT_UNREPLIED
] = un
->timeouts
[UDP_CT_UNREPLIED
];
169 timeouts
[UDP_CT_REPLIED
] = un
->timeouts
[UDP_CT_REPLIED
];
171 if (tb
[CTA_TIMEOUT_UDP_UNREPLIED
]) {
172 timeouts
[UDP_CT_UNREPLIED
] =
173 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_UDP_UNREPLIED
])) * HZ
;
175 if (tb
[CTA_TIMEOUT_UDP_REPLIED
]) {
176 timeouts
[UDP_CT_REPLIED
] =
177 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_UDP_REPLIED
])) * HZ
;
183 udp_timeout_obj_to_nlattr(struct sk_buff
*skb
, const void *data
)
185 const unsigned int *timeouts
= data
;
187 if (nla_put_be32(skb
, CTA_TIMEOUT_UDP_UNREPLIED
,
188 htonl(timeouts
[UDP_CT_UNREPLIED
] / HZ
)) ||
189 nla_put_be32(skb
, CTA_TIMEOUT_UDP_REPLIED
,
190 htonl(timeouts
[UDP_CT_REPLIED
] / HZ
)))
191 goto nla_put_failure
;
198 static const struct nla_policy
199 udp_timeout_nla_policy
[CTA_TIMEOUT_UDP_MAX
+1] = {
200 [CTA_TIMEOUT_UDP_UNREPLIED
] = { .type
= NLA_U32
},
201 [CTA_TIMEOUT_UDP_REPLIED
] = { .type
= NLA_U32
},
203 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
206 static struct ctl_table udp_sysctl_table
[] = {
208 .procname
= "nf_conntrack_udp_timeout",
209 .maxlen
= sizeof(unsigned int),
211 .proc_handler
= proc_dointvec_jiffies
,
214 .procname
= "nf_conntrack_udp_timeout_stream",
215 .maxlen
= sizeof(unsigned int),
217 .proc_handler
= proc_dointvec_jiffies
,
221 #endif /* CONFIG_SYSCTL */
223 static int udp_kmemdup_sysctl_table(struct nf_proto_net
*pn
,
224 struct nf_udp_net
*un
)
229 pn
->ctl_table
= kmemdup(udp_sysctl_table
,
230 sizeof(udp_sysctl_table
),
234 pn
->ctl_table
[0].data
= &un
->timeouts
[UDP_CT_UNREPLIED
];
235 pn
->ctl_table
[1].data
= &un
->timeouts
[UDP_CT_REPLIED
];
240 static int udp_init_net(struct net
*net
, u_int16_t proto
)
242 struct nf_udp_net
*un
= udp_pernet(net
);
243 struct nf_proto_net
*pn
= &un
->pn
;
248 for (i
= 0; i
< UDP_CT_MAX
; i
++)
249 un
->timeouts
[i
] = udp_timeouts
[i
];
252 return udp_kmemdup_sysctl_table(pn
, un
);
255 static struct nf_proto_net
*udp_get_net_proto(struct net
*net
)
257 return &net
->ct
.nf_ct_proto
.udp
.pn
;
260 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly
=
263 .l4proto
= IPPROTO_UDP
,
266 .pkt_to_tuple
= udp_pkt_to_tuple
,
267 .invert_tuple
= udp_invert_tuple
,
268 .print_tuple
= udp_print_tuple
,
269 .packet
= udp_packet
,
270 .get_timeouts
= udp_get_timeouts
,
273 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
274 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
275 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
276 .nlattr_tuple_size
= nf_ct_port_nlattr_tuple_size
,
277 .nla_policy
= nf_ct_port_nla_policy
,
279 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
281 .nlattr_to_obj
= udp_timeout_nlattr_to_obj
,
282 .obj_to_nlattr
= udp_timeout_obj_to_nlattr
,
283 .nlattr_max
= CTA_TIMEOUT_UDP_MAX
,
284 .obj_size
= sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX
,
285 .nla_policy
= udp_timeout_nla_policy
,
287 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
288 .init_net
= udp_init_net
,
289 .get_net_proto
= udp_get_net_proto
,
291 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4
);
293 struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly
=
296 .l4proto
= IPPROTO_UDP
,
299 .pkt_to_tuple
= udp_pkt_to_tuple
,
300 .invert_tuple
= udp_invert_tuple
,
301 .print_tuple
= udp_print_tuple
,
302 .packet
= udp_packet
,
303 .get_timeouts
= udp_get_timeouts
,
306 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
307 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
308 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
309 .nlattr_tuple_size
= nf_ct_port_nlattr_tuple_size
,
310 .nla_policy
= nf_ct_port_nla_policy
,
312 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
314 .nlattr_to_obj
= udp_timeout_nlattr_to_obj
,
315 .obj_to_nlattr
= udp_timeout_obj_to_nlattr
,
316 .nlattr_max
= CTA_TIMEOUT_UDP_MAX
,
317 .obj_size
= sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX
,
318 .nla_policy
= udp_timeout_nla_policy
,
320 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
321 .init_net
= udp_init_net
,
322 .get_net_proto
= udp_get_net_proto
,
324 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6
);