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