]>
Commit | Line | Data |
---|---|---|
59eecdfb PM |
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> | |
4 | * | |
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. | |
8 | */ | |
9 | ||
10 | #include <linux/types.h> | |
11 | #include <linux/timer.h> | |
59eecdfb PM |
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> | |
18 | ||
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> | |
f01ffbd6 | 24 | #include <net/netfilter/nf_log.h> |
59eecdfb | 25 | |
5a41db94 PNA |
26 | static unsigned int udplite_timeouts[UDPLITE_CT_MAX] = { |
27 | [UDPLITE_CT_UNREPLIED] = 30*HZ, | |
28 | [UDPLITE_CT_REPLIED] = 180*HZ, | |
29 | }; | |
59eecdfb | 30 | |
9b91c96c | 31 | static inline struct nf_udplite_net *udplite_pernet(struct net *net) |
a8021fed | 32 | { |
9b91c96c | 33 | return &net->ct.nf_ct_proto.udplite; |
a8021fed G |
34 | } |
35 | ||
09f263cd JE |
36 | static bool udplite_pkt_to_tuple(const struct sk_buff *skb, |
37 | unsigned int dataoff, | |
a31f1adc | 38 | struct net *net, |
09f263cd | 39 | struct nf_conntrack_tuple *tuple) |
59eecdfb | 40 | { |
da3f13c9 JE |
41 | const struct udphdr *hp; |
42 | struct udphdr _hdr; | |
59eecdfb | 43 | |
e5e693ab GF |
44 | /* Actually only need first 4 bytes to get ports. */ |
45 | hp = skb_header_pointer(skb, dataoff, 4, &_hdr); | |
59eecdfb | 46 | if (hp == NULL) |
09f263cd | 47 | return false; |
59eecdfb PM |
48 | |
49 | tuple->src.u.udp.port = hp->source; | |
50 | tuple->dst.u.udp.port = hp->dest; | |
09f263cd | 51 | return true; |
59eecdfb PM |
52 | } |
53 | ||
09f263cd JE |
54 | static bool udplite_invert_tuple(struct nf_conntrack_tuple *tuple, |
55 | const struct nf_conntrack_tuple *orig) | |
59eecdfb PM |
56 | { |
57 | tuple->src.u.udp.port = orig->dst.u.udp.port; | |
58 | tuple->dst.u.udp.port = orig->src.u.udp.port; | |
09f263cd | 59 | return true; |
59eecdfb PM |
60 | } |
61 | ||
62 | /* Print out the per-protocol part of the tuple. */ | |
824f1fbe JP |
63 | static void udplite_print_tuple(struct seq_file *s, |
64 | const struct nf_conntrack_tuple *tuple) | |
59eecdfb | 65 | { |
824f1fbe JP |
66 | seq_printf(s, "sport=%hu dport=%hu ", |
67 | ntohs(tuple->src.u.udp.port), | |
68 | ntohs(tuple->dst.u.udp.port)); | |
59eecdfb PM |
69 | } |
70 | ||
2c8503f5 PNA |
71 | static unsigned int *udplite_get_timeouts(struct net *net) |
72 | { | |
a8021fed | 73 | return udplite_pernet(net)->timeouts; |
2c8503f5 PNA |
74 | } |
75 | ||
59eecdfb | 76 | /* Returns verdict for packet, and may modify conntracktype */ |
c88130bc | 77 | static int udplite_packet(struct nf_conn *ct, |
59eecdfb PM |
78 | const struct sk_buff *skb, |
79 | unsigned int dataoff, | |
80 | enum ip_conntrack_info ctinfo, | |
76108cea | 81 | u_int8_t pf, |
2c8503f5 PNA |
82 | unsigned int hooknum, |
83 | unsigned int *timeouts) | |
59eecdfb PM |
84 | { |
85 | /* If we've seen traffic both ways, this is some kind of UDP | |
86 | stream. Extend timeout. */ | |
c88130bc PM |
87 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { |
88 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
2c8503f5 | 89 | timeouts[UDPLITE_CT_REPLIED]); |
59eecdfb | 90 | /* Also, more likely to be important, and not a probe */ |
c88130bc | 91 | if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status)) |
858b3133 | 92 | nf_conntrack_event_cache(IPCT_ASSURED, ct); |
5a41db94 PNA |
93 | } else { |
94 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
2c8503f5 | 95 | timeouts[UDPLITE_CT_UNREPLIED]); |
5a41db94 | 96 | } |
59eecdfb PM |
97 | return NF_ACCEPT; |
98 | } | |
99 | ||
100 | /* Called when a new connection for this protocol found. */ | |
09f263cd | 101 | static bool udplite_new(struct nf_conn *ct, const struct sk_buff *skb, |
2c8503f5 | 102 | unsigned int dataoff, unsigned int *timeouts) |
59eecdfb | 103 | { |
09f263cd | 104 | return true; |
59eecdfb PM |
105 | } |
106 | ||
8fea97ec | 107 | static int udplite_error(struct net *net, struct nf_conn *tmpl, |
74c51a14 AD |
108 | struct sk_buff *skb, |
109 | unsigned int dataoff, | |
59eecdfb | 110 | enum ip_conntrack_info *ctinfo, |
76108cea | 111 | u_int8_t pf, |
59eecdfb PM |
112 | unsigned int hooknum) |
113 | { | |
114 | unsigned int udplen = skb->len - dataoff; | |
da3f13c9 JE |
115 | const struct udphdr *hdr; |
116 | struct udphdr _hdr; | |
59eecdfb PM |
117 | unsigned int cscov; |
118 | ||
119 | /* Header is too small? */ | |
120 | hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); | |
121 | if (hdr == NULL) { | |
c2a2c7e0 | 122 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
30e0c6a6 | 123 | nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL, |
59eecdfb PM |
124 | "nf_ct_udplite: short packet "); |
125 | return -NF_ACCEPT; | |
126 | } | |
127 | ||
128 | cscov = ntohs(hdr->len); | |
129 | if (cscov == 0) | |
130 | cscov = udplen; | |
131 | else if (cscov < sizeof(*hdr) || cscov > udplen) { | |
c2a2c7e0 | 132 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
30e0c6a6 | 133 | nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL, |
59eecdfb PM |
134 | "nf_ct_udplite: invalid checksum coverage "); |
135 | return -NF_ACCEPT; | |
136 | } | |
137 | ||
138 | /* UDPLITE mandates checksums */ | |
139 | if (!hdr->check) { | |
c2a2c7e0 | 140 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
30e0c6a6 | 141 | nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL, |
59eecdfb PM |
142 | "nf_ct_udplite: checksum missing "); |
143 | return -NF_ACCEPT; | |
144 | } | |
145 | ||
146 | /* Checksum invalid? Ignore. */ | |
c04d0552 | 147 | if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING && |
d63a6507 PM |
148 | nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_UDP, |
149 | pf)) { | |
c2a2c7e0 | 150 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
30e0c6a6 | 151 | nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL, |
d63a6507 PM |
152 | "nf_ct_udplite: bad UDPLite checksum "); |
153 | return -NF_ACCEPT; | |
59eecdfb PM |
154 | } |
155 | ||
156 | return NF_ACCEPT; | |
157 | } | |
158 | ||
50978462 PNA |
159 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
160 | ||
161 | #include <linux/netfilter/nfnetlink.h> | |
162 | #include <linux/netfilter/nfnetlink_cttimeout.h> | |
163 | ||
8264deb8 G |
164 | static int udplite_timeout_nlattr_to_obj(struct nlattr *tb[], |
165 | struct net *net, void *data) | |
50978462 PNA |
166 | { |
167 | unsigned int *timeouts = data; | |
9b91c96c | 168 | struct nf_udplite_net *un = udplite_pernet(net); |
50978462 PNA |
169 | |
170 | /* set default timeouts for UDPlite. */ | |
8264deb8 G |
171 | timeouts[UDPLITE_CT_UNREPLIED] = un->timeouts[UDPLITE_CT_UNREPLIED]; |
172 | timeouts[UDPLITE_CT_REPLIED] = un->timeouts[UDPLITE_CT_REPLIED]; | |
50978462 PNA |
173 | |
174 | if (tb[CTA_TIMEOUT_UDPLITE_UNREPLIED]) { | |
175 | timeouts[UDPLITE_CT_UNREPLIED] = | |
176 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_UNREPLIED])) * HZ; | |
177 | } | |
178 | if (tb[CTA_TIMEOUT_UDPLITE_REPLIED]) { | |
179 | timeouts[UDPLITE_CT_REPLIED] = | |
180 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDPLITE_REPLIED])) * HZ; | |
181 | } | |
182 | return 0; | |
183 | } | |
184 | ||
185 | static int | |
186 | udplite_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data) | |
187 | { | |
188 | const unsigned int *timeouts = data; | |
189 | ||
3c60a17b DM |
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; | |
50978462 PNA |
195 | return 0; |
196 | ||
197 | nla_put_failure: | |
198 | return -ENOSPC; | |
199 | } | |
200 | ||
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 }, | |
205 | }; | |
206 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
207 | ||
59eecdfb | 208 | #ifdef CONFIG_SYSCTL |
59eecdfb PM |
209 | static struct ctl_table udplite_sysctl_table[] = { |
210 | { | |
59eecdfb | 211 | .procname = "nf_conntrack_udplite_timeout", |
59eecdfb PM |
212 | .maxlen = sizeof(unsigned int), |
213 | .mode = 0644, | |
6d9f239a | 214 | .proc_handler = proc_dointvec_jiffies, |
59eecdfb PM |
215 | }, |
216 | { | |
59eecdfb | 217 | .procname = "nf_conntrack_udplite_timeout_stream", |
59eecdfb PM |
218 | .maxlen = sizeof(unsigned int), |
219 | .mode = 0644, | |
6d9f239a | 220 | .proc_handler = proc_dointvec_jiffies, |
59eecdfb | 221 | }, |
f8572d8f | 222 | { } |
59eecdfb PM |
223 | }; |
224 | #endif /* CONFIG_SYSCTL */ | |
225 | ||
51b4c824 | 226 | static int udplite_kmemdup_sysctl_table(struct nf_proto_net *pn, |
9b91c96c | 227 | struct nf_udplite_net *un) |
a8021fed | 228 | { |
a8021fed | 229 | #ifdef CONFIG_SYSCTL |
51b4c824 G |
230 | if (pn->ctl_table) |
231 | return 0; | |
232 | ||
233 | pn->ctl_table = kmemdup(udplite_sysctl_table, | |
234 | sizeof(udplite_sysctl_table), | |
235 | GFP_KERNEL); | |
236 | if (!pn->ctl_table) | |
237 | return -ENOMEM; | |
238 | ||
239 | pn->ctl_table[0].data = &un->timeouts[UDPLITE_CT_UNREPLIED]; | |
240 | pn->ctl_table[1].data = &un->timeouts[UDPLITE_CT_REPLIED]; | |
a8021fed | 241 | #endif |
51b4c824 G |
242 | return 0; |
243 | } | |
244 | ||
245 | static int udplite_init_net(struct net *net, u_int16_t proto) | |
246 | { | |
9b91c96c | 247 | struct nf_udplite_net *un = udplite_pernet(net); |
51b4c824 G |
248 | struct nf_proto_net *pn = &un->pn; |
249 | ||
250 | if (!pn->users) { | |
251 | int i; | |
252 | ||
a8021fed G |
253 | for (i = 0 ; i < UDPLITE_CT_MAX; i++) |
254 | un->timeouts[i] = udplite_timeouts[i]; | |
a8021fed | 255 | } |
51b4c824 G |
256 | |
257 | return udplite_kmemdup_sysctl_table(pn, un); | |
a8021fed G |
258 | } |
259 | ||
9b91c96c | 260 | struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly = |
59eecdfb PM |
261 | { |
262 | .l3proto = PF_INET, | |
263 | .l4proto = IPPROTO_UDPLITE, | |
264 | .name = "udplite", | |
71d8c47f | 265 | .allow_clash = true, |
59eecdfb PM |
266 | .pkt_to_tuple = udplite_pkt_to_tuple, |
267 | .invert_tuple = udplite_invert_tuple, | |
268 | .print_tuple = udplite_print_tuple, | |
59eecdfb | 269 | .packet = udplite_packet, |
2c8503f5 | 270 | .get_timeouts = udplite_get_timeouts, |
59eecdfb PM |
271 | .new = udplite_new, |
272 | .error = udplite_error, | |
c0cd1156 | 273 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 | 274 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
a400c30e | 275 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
fdf70832 | 276 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
f73e924c | 277 | .nla_policy = nf_ct_port_nla_policy, |
59eecdfb | 278 | #endif |
50978462 PNA |
279 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
280 | .ctnl_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, | |
287 | }, | |
288 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
a8021fed | 289 | .init_net = udplite_init_net, |
59eecdfb | 290 | }; |
9b91c96c | 291 | EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udplite4); |
59eecdfb | 292 | |
9b91c96c | 293 | struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly = |
59eecdfb PM |
294 | { |
295 | .l3proto = PF_INET6, | |
296 | .l4proto = IPPROTO_UDPLITE, | |
297 | .name = "udplite", | |
71d8c47f | 298 | .allow_clash = true, |
59eecdfb PM |
299 | .pkt_to_tuple = udplite_pkt_to_tuple, |
300 | .invert_tuple = udplite_invert_tuple, | |
301 | .print_tuple = udplite_print_tuple, | |
59eecdfb | 302 | .packet = udplite_packet, |
2c8503f5 | 303 | .get_timeouts = udplite_get_timeouts, |
59eecdfb PM |
304 | .new = udplite_new, |
305 | .error = udplite_error, | |
c0cd1156 | 306 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 | 307 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
5ff48294 | 308 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
fdf70832 | 309 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
f73e924c | 310 | .nla_policy = nf_ct_port_nla_policy, |
59eecdfb | 311 | #endif |
50978462 PNA |
312 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
313 | .ctnl_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, | |
320 | }, | |
321 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
a8021fed | 322 | .init_net = udplite_init_net, |
59eecdfb | 323 | }; |
9b91c96c | 324 | EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udplite6); |