Commit | Line | Data |
---|---|---|
9fb9cbb1 YK |
1 | /* (C) 1999-2001 Paul `Rusty' Russell |
2 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
9fb9cbb1 YK |
7 | */ |
8 | ||
9 | #include <linux/types.h> | |
9fb9cbb1 YK |
10 | #include <linux/timer.h> |
11 | #include <linux/module.h> | |
9fb9cbb1 YK |
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> | |
f6180121 | 18 | |
9fb9cbb1 YK |
19 | #include <linux/netfilter.h> |
20 | #include <linux/netfilter_ipv4.h> | |
21 | #include <linux/netfilter_ipv6.h> | |
605dcad6 | 22 | #include <net/netfilter/nf_conntrack_l4proto.h> |
f6180121 | 23 | #include <net/netfilter/nf_conntrack_ecache.h> |
f01ffbd6 | 24 | #include <net/netfilter/nf_log.h> |
9d2493f8 CP |
25 | #include <net/netfilter/ipv4/nf_conntrack_ipv4.h> |
26 | #include <net/netfilter/ipv6/nf_conntrack_ipv6.h> | |
9fb9cbb1 | 27 | |
933a41e7 PM |
28 | static unsigned int nf_ct_udp_timeout __read_mostly = 30*HZ; |
29 | static unsigned int nf_ct_udp_timeout_stream __read_mostly = 180*HZ; | |
9fb9cbb1 | 30 | |
09f263cd | 31 | static bool udp_pkt_to_tuple(const struct sk_buff *skb, |
9fb9cbb1 YK |
32 | unsigned int dataoff, |
33 | struct nf_conntrack_tuple *tuple) | |
34 | { | |
da3f13c9 JE |
35 | const struct udphdr *hp; |
36 | struct udphdr _hdr; | |
9fb9cbb1 YK |
37 | |
38 | /* Actually only need first 8 bytes. */ | |
39 | hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); | |
40 | if (hp == NULL) | |
09f263cd | 41 | return false; |
9fb9cbb1 YK |
42 | |
43 | tuple->src.u.udp.port = hp->source; | |
44 | tuple->dst.u.udp.port = hp->dest; | |
45 | ||
09f263cd | 46 | return true; |
9fb9cbb1 YK |
47 | } |
48 | ||
09f263cd JE |
49 | static bool udp_invert_tuple(struct nf_conntrack_tuple *tuple, |
50 | const struct nf_conntrack_tuple *orig) | |
9fb9cbb1 YK |
51 | { |
52 | tuple->src.u.udp.port = orig->dst.u.udp.port; | |
53 | tuple->dst.u.udp.port = orig->src.u.udp.port; | |
09f263cd | 54 | return true; |
9fb9cbb1 YK |
55 | } |
56 | ||
57 | /* Print out the per-protocol part of the tuple. */ | |
58 | static int udp_print_tuple(struct seq_file *s, | |
59 | const struct nf_conntrack_tuple *tuple) | |
60 | { | |
61 | return seq_printf(s, "sport=%hu dport=%hu ", | |
62 | ntohs(tuple->src.u.udp.port), | |
63 | ntohs(tuple->dst.u.udp.port)); | |
64 | } | |
65 | ||
9fb9cbb1 | 66 | /* Returns verdict for packet, and may modify conntracktype */ |
c88130bc | 67 | static int udp_packet(struct nf_conn *ct, |
9fb9cbb1 YK |
68 | const struct sk_buff *skb, |
69 | unsigned int dataoff, | |
70 | enum ip_conntrack_info ctinfo, | |
76108cea | 71 | u_int8_t pf, |
9fb9cbb1 YK |
72 | unsigned int hooknum) |
73 | { | |
74 | /* If we've seen traffic both ways, this is some kind of UDP | |
75 | stream. Extend timeout. */ | |
c88130bc PM |
76 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { |
77 | nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_udp_timeout_stream); | |
9fb9cbb1 | 78 | /* Also, more likely to be important, and not a probe */ |
c88130bc | 79 | if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status)) |
a71996fc | 80 | nf_conntrack_event_cache(IPCT_STATUS, ct); |
9fb9cbb1 | 81 | } else |
c88130bc | 82 | nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_udp_timeout); |
9fb9cbb1 YK |
83 | |
84 | return NF_ACCEPT; | |
85 | } | |
86 | ||
87 | /* Called when a new connection for this protocol found. */ | |
09f263cd JE |
88 | static bool udp_new(struct nf_conn *ct, const struct sk_buff *skb, |
89 | unsigned int dataoff) | |
9fb9cbb1 | 90 | { |
09f263cd | 91 | return true; |
9fb9cbb1 YK |
92 | } |
93 | ||
74c51a14 | 94 | static int udp_error(struct net *net, struct sk_buff *skb, unsigned int dataoff, |
9fb9cbb1 | 95 | enum ip_conntrack_info *ctinfo, |
76108cea | 96 | u_int8_t pf, |
96f6bf82 | 97 | unsigned int hooknum) |
9fb9cbb1 YK |
98 | { |
99 | unsigned int udplen = skb->len - dataoff; | |
da3f13c9 JE |
100 | const struct udphdr *hdr; |
101 | struct udphdr _hdr; | |
9fb9cbb1 YK |
102 | |
103 | /* Header is too small? */ | |
104 | hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); | |
105 | if (hdr == NULL) { | |
c2a2c7e0 | 106 | if (LOG_INVALID(net, IPPROTO_UDP)) |
9fb9cbb1 YK |
107 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
108 | "nf_ct_udp: short packet "); | |
109 | return -NF_ACCEPT; | |
110 | } | |
111 | ||
112 | /* Truncated/malformed packets */ | |
113 | if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) { | |
c2a2c7e0 | 114 | if (LOG_INVALID(net, IPPROTO_UDP)) |
9fb9cbb1 YK |
115 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
116 | "nf_ct_udp: truncated/malformed packet "); | |
117 | return -NF_ACCEPT; | |
118 | } | |
119 | ||
120 | /* Packet with no checksum */ | |
121 | if (!hdr->check) | |
122 | return NF_ACCEPT; | |
123 | ||
124 | /* Checksum invalid? Ignore. | |
125 | * We skip checking packets on the outgoing path | |
84fa7933 | 126 | * because the checksum is assumed to be correct. |
9fb9cbb1 | 127 | * FIXME: Source route IP option packets --RR */ |
c04d0552 | 128 | if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING && |
96f6bf82 | 129 | nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) { |
c2a2c7e0 | 130 | if (LOG_INVALID(net, IPPROTO_UDP)) |
9fb9cbb1 YK |
131 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
132 | "nf_ct_udp: bad UDP checksum "); | |
133 | return -NF_ACCEPT; | |
134 | } | |
135 | ||
136 | return NF_ACCEPT; | |
137 | } | |
138 | ||
933a41e7 PM |
139 | #ifdef CONFIG_SYSCTL |
140 | static unsigned int udp_sysctl_table_users; | |
141 | static struct ctl_table_header *udp_sysctl_header; | |
142 | static struct ctl_table udp_sysctl_table[] = { | |
143 | { | |
933a41e7 PM |
144 | .procname = "nf_conntrack_udp_timeout", |
145 | .data = &nf_ct_udp_timeout, | |
146 | .maxlen = sizeof(unsigned int), | |
147 | .mode = 0644, | |
6d9f239a | 148 | .proc_handler = proc_dointvec_jiffies, |
933a41e7 PM |
149 | }, |
150 | { | |
933a41e7 PM |
151 | .procname = "nf_conntrack_udp_timeout_stream", |
152 | .data = &nf_ct_udp_timeout_stream, | |
153 | .maxlen = sizeof(unsigned int), | |
154 | .mode = 0644, | |
6d9f239a | 155 | .proc_handler = proc_dointvec_jiffies, |
933a41e7 PM |
156 | }, |
157 | { | |
158 | .ctl_name = 0 | |
159 | } | |
160 | }; | |
a999e683 PM |
161 | #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT |
162 | static struct ctl_table udp_compat_sysctl_table[] = { | |
163 | { | |
a999e683 PM |
164 | .procname = "ip_conntrack_udp_timeout", |
165 | .data = &nf_ct_udp_timeout, | |
166 | .maxlen = sizeof(unsigned int), | |
167 | .mode = 0644, | |
6d9f239a | 168 | .proc_handler = proc_dointvec_jiffies, |
a999e683 PM |
169 | }, |
170 | { | |
a999e683 PM |
171 | .procname = "ip_conntrack_udp_timeout_stream", |
172 | .data = &nf_ct_udp_timeout_stream, | |
173 | .maxlen = sizeof(unsigned int), | |
174 | .mode = 0644, | |
6d9f239a | 175 | .proc_handler = proc_dointvec_jiffies, |
a999e683 PM |
176 | }, |
177 | { | |
178 | .ctl_name = 0 | |
179 | } | |
180 | }; | |
181 | #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */ | |
933a41e7 PM |
182 | #endif /* CONFIG_SYSCTL */ |
183 | ||
61075af5 | 184 | struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly = |
9fb9cbb1 YK |
185 | { |
186 | .l3proto = PF_INET, | |
605dcad6 | 187 | .l4proto = IPPROTO_UDP, |
9fb9cbb1 YK |
188 | .name = "udp", |
189 | .pkt_to_tuple = udp_pkt_to_tuple, | |
190 | .invert_tuple = udp_invert_tuple, | |
191 | .print_tuple = udp_print_tuple, | |
9fb9cbb1 YK |
192 | .packet = udp_packet, |
193 | .new = udp_new, | |
96f6bf82 | 194 | .error = udp_error, |
e281db5c | 195 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
fdf70832 PM |
196 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
197 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, | |
a400c30e | 198 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
f73e924c | 199 | .nla_policy = nf_ct_port_nla_policy, |
c1d10adb | 200 | #endif |
933a41e7 PM |
201 | #ifdef CONFIG_SYSCTL |
202 | .ctl_table_users = &udp_sysctl_table_users, | |
203 | .ctl_table_header = &udp_sysctl_header, | |
204 | .ctl_table = udp_sysctl_table, | |
a999e683 PM |
205 | #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT |
206 | .ctl_compat_table = udp_compat_sysctl_table, | |
207 | #endif | |
933a41e7 | 208 | #endif |
9fb9cbb1 | 209 | }; |
13b18339 | 210 | EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4); |
9fb9cbb1 | 211 | |
61075af5 | 212 | struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly = |
9fb9cbb1 YK |
213 | { |
214 | .l3proto = PF_INET6, | |
605dcad6 | 215 | .l4proto = IPPROTO_UDP, |
9fb9cbb1 YK |
216 | .name = "udp", |
217 | .pkt_to_tuple = udp_pkt_to_tuple, | |
218 | .invert_tuple = udp_invert_tuple, | |
219 | .print_tuple = udp_print_tuple, | |
9fb9cbb1 YK |
220 | .packet = udp_packet, |
221 | .new = udp_new, | |
96f6bf82 | 222 | .error = udp_error, |
e281db5c | 223 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
fdf70832 PM |
224 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
225 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, | |
a400c30e | 226 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
f73e924c | 227 | .nla_policy = nf_ct_port_nla_policy, |
c1d10adb | 228 | #endif |
933a41e7 PM |
229 | #ifdef CONFIG_SYSCTL |
230 | .ctl_table_users = &udp_sysctl_table_users, | |
231 | .ctl_table_header = &udp_sysctl_header, | |
232 | .ctl_table = udp_sysctl_table, | |
233 | #endif | |
9fb9cbb1 | 234 | }; |
13b18339 | 235 | EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6); |