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