blob: 1fa62f3c24f190dd10d958bc84871c59357c2c10 [file] [log] [blame]
Patrick McHardy59eecdf2007-07-14 20:48:44 -07001/* (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>
12#include <linux/module.h>
Patrick McHardy59eecdf2007-07-14 20:48:44 -070013#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>
19
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>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080025#include <net/netfilter/nf_log.h>
Patrick McHardy59eecdf2007-07-14 20:48:44 -070026
27static unsigned int nf_ct_udplite_timeout __read_mostly = 30*HZ;
28static unsigned int nf_ct_udplite_timeout_stream __read_mostly = 180*HZ;
29
Jan Engelhardt09f263c2008-04-14 11:15:53 +020030static bool udplite_pkt_to_tuple(const struct sk_buff *skb,
31 unsigned int dataoff,
32 struct nf_conntrack_tuple *tuple)
Patrick McHardy59eecdf2007-07-14 20:48:44 -070033{
Jan Engelhardtda3f13c2008-01-31 04:52:29 -080034 const struct udphdr *hp;
35 struct udphdr _hdr;
Patrick McHardy59eecdf2007-07-14 20:48:44 -070036
37 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
38 if (hp == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +020039 return false;
Patrick McHardy59eecdf2007-07-14 20:48:44 -070040
41 tuple->src.u.udp.port = hp->source;
42 tuple->dst.u.udp.port = hp->dest;
Jan Engelhardt09f263c2008-04-14 11:15:53 +020043 return true;
Patrick McHardy59eecdf2007-07-14 20:48:44 -070044}
45
Jan Engelhardt09f263c2008-04-14 11:15:53 +020046static bool udplite_invert_tuple(struct nf_conntrack_tuple *tuple,
47 const struct nf_conntrack_tuple *orig)
Patrick McHardy59eecdf2007-07-14 20:48:44 -070048{
49 tuple->src.u.udp.port = orig->dst.u.udp.port;
50 tuple->dst.u.udp.port = orig->src.u.udp.port;
Jan Engelhardt09f263c2008-04-14 11:15:53 +020051 return true;
Patrick McHardy59eecdf2007-07-14 20:48:44 -070052}
53
54/* Print out the per-protocol part of the tuple. */
55static int udplite_print_tuple(struct seq_file *s,
56 const struct nf_conntrack_tuple *tuple)
57{
58 return seq_printf(s, "sport=%hu dport=%hu ",
59 ntohs(tuple->src.u.udp.port),
60 ntohs(tuple->dst.u.udp.port));
61}
62
Patrick McHardy59eecdf2007-07-14 20:48:44 -070063/* Returns verdict for packet, and may modify conntracktype */
Patrick McHardyc88130b2008-01-31 04:42:11 -080064static int udplite_packet(struct nf_conn *ct,
Patrick McHardy59eecdf2007-07-14 20:48:44 -070065 const struct sk_buff *skb,
66 unsigned int dataoff,
67 enum ip_conntrack_info ctinfo,
68 int pf,
69 unsigned int hooknum)
70{
71 /* If we've seen traffic both ways, this is some kind of UDP
72 stream. Extend timeout. */
Patrick McHardyc88130b2008-01-31 04:42:11 -080073 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
74 nf_ct_refresh_acct(ct, ctinfo, skb,
Patrick McHardy59eecdf2007-07-14 20:48:44 -070075 nf_ct_udplite_timeout_stream);
76 /* Also, more likely to be important, and not a probe */
Patrick McHardyc88130b2008-01-31 04:42:11 -080077 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
Patrick McHardy59eecdf2007-07-14 20:48:44 -070078 nf_conntrack_event_cache(IPCT_STATUS, skb);
79 } else
Patrick McHardyc88130b2008-01-31 04:42:11 -080080 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_udplite_timeout);
Patrick McHardy59eecdf2007-07-14 20:48:44 -070081
82 return NF_ACCEPT;
83}
84
85/* Called when a new connection for this protocol found. */
Jan Engelhardt09f263c2008-04-14 11:15:53 +020086static bool udplite_new(struct nf_conn *ct, const struct sk_buff *skb,
87 unsigned int dataoff)
Patrick McHardy59eecdf2007-07-14 20:48:44 -070088{
Jan Engelhardt09f263c2008-04-14 11:15:53 +020089 return true;
Patrick McHardy59eecdf2007-07-14 20:48:44 -070090}
91
92static int udplite_error(struct sk_buff *skb, unsigned int dataoff,
93 enum ip_conntrack_info *ctinfo,
94 int pf,
95 unsigned int hooknum)
96{
97 unsigned int udplen = skb->len - dataoff;
Jan Engelhardtda3f13c2008-01-31 04:52:29 -080098 const struct udphdr *hdr;
99 struct udphdr _hdr;
Patrick McHardy59eecdf2007-07-14 20:48:44 -0700100 unsigned int cscov;
101
102 /* Header is too small? */
103 hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
104 if (hdr == NULL) {
105 if (LOG_INVALID(IPPROTO_UDPLITE))
106 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
107 "nf_ct_udplite: short packet ");
108 return -NF_ACCEPT;
109 }
110
111 cscov = ntohs(hdr->len);
112 if (cscov == 0)
113 cscov = udplen;
114 else if (cscov < sizeof(*hdr) || cscov > udplen) {
115 if (LOG_INVALID(IPPROTO_UDPLITE))
116 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
117 "nf_ct_udplite: invalid checksum coverage ");
118 return -NF_ACCEPT;
119 }
120
121 /* UDPLITE mandates checksums */
122 if (!hdr->check) {
123 if (LOG_INVALID(IPPROTO_UDPLITE))
124 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
125 "nf_ct_udplite: checksum missing ");
126 return -NF_ACCEPT;
127 }
128
129 /* Checksum invalid? Ignore. */
Patrick McHardyd63a6502008-03-20 15:15:53 +0100130 if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING &&
131 nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_UDP,
132 pf)) {
133 if (LOG_INVALID(IPPROTO_UDPLITE))
134 nf_log_packet(pf, 0, skb, NULL, NULL, NULL,
135 "nf_ct_udplite: bad UDPLite checksum ");
136 return -NF_ACCEPT;
Patrick McHardy59eecdf2007-07-14 20:48:44 -0700137 }
138
139 return NF_ACCEPT;
140}
141
142#ifdef CONFIG_SYSCTL
143static unsigned int udplite_sysctl_table_users;
144static struct ctl_table_header *udplite_sysctl_header;
145static struct ctl_table udplite_sysctl_table[] = {
146 {
147 .ctl_name = CTL_UNNUMBERED,
148 .procname = "nf_conntrack_udplite_timeout",
149 .data = &nf_ct_udplite_timeout,
150 .maxlen = sizeof(unsigned int),
151 .mode = 0644,
152 .proc_handler = &proc_dointvec_jiffies,
153 },
154 {
155 .ctl_name = CTL_UNNUMBERED,
156 .procname = "nf_conntrack_udplite_timeout_stream",
157 .data = &nf_ct_udplite_timeout_stream,
158 .maxlen = sizeof(unsigned int),
159 .mode = 0644,
160 .proc_handler = &proc_dointvec_jiffies,
161 },
162 {
163 .ctl_name = 0
164 }
165};
166#endif /* CONFIG_SYSCTL */
167
168static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly =
169{
170 .l3proto = PF_INET,
171 .l4proto = IPPROTO_UDPLITE,
172 .name = "udplite",
173 .pkt_to_tuple = udplite_pkt_to_tuple,
174 .invert_tuple = udplite_invert_tuple,
175 .print_tuple = udplite_print_tuple,
Patrick McHardy59eecdf2007-07-14 20:48:44 -0700176 .packet = udplite_packet,
177 .new = udplite_new,
178 .error = udplite_error,
179#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700180 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
181 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700182 .nla_policy = nf_ct_port_nla_policy,
Patrick McHardy59eecdf2007-07-14 20:48:44 -0700183#endif
184#ifdef CONFIG_SYSCTL
185 .ctl_table_users = &udplite_sysctl_table_users,
186 .ctl_table_header = &udplite_sysctl_header,
187 .ctl_table = udplite_sysctl_table,
188#endif
189};
190
191static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
192{
193 .l3proto = PF_INET6,
194 .l4proto = IPPROTO_UDPLITE,
195 .name = "udplite",
196 .pkt_to_tuple = udplite_pkt_to_tuple,
197 .invert_tuple = udplite_invert_tuple,
198 .print_tuple = udplite_print_tuple,
Patrick McHardy59eecdf2007-07-14 20:48:44 -0700199 .packet = udplite_packet,
200 .new = udplite_new,
201 .error = udplite_error,
202#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700203 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
204 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700205 .nla_policy = nf_ct_port_nla_policy,
Patrick McHardy59eecdf2007-07-14 20:48:44 -0700206#endif
207#ifdef CONFIG_SYSCTL
208 .ctl_table_users = &udplite_sysctl_table_users,
209 .ctl_table_header = &udplite_sysctl_header,
210 .ctl_table = udplite_sysctl_table,
211#endif
212};
213
214static int __init nf_conntrack_proto_udplite_init(void)
215{
216 int err;
217
218 err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite4);
219 if (err < 0)
220 goto err1;
221 err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite6);
222 if (err < 0)
223 goto err2;
224 return 0;
225err2:
226 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
227err1:
228 return err;
229}
230
231static void __exit nf_conntrack_proto_udplite_exit(void)
232{
233 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite6);
234 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4);
235}
236
237module_init(nf_conntrack_proto_udplite_init);
238module_exit(nf_conntrack_proto_udplite_exit);
239
240MODULE_LICENSE("GPL");