]>
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. | |
7 | * | |
8 | * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> | |
9 | * - enable working with Layer 3 protocol independent connection tracking. | |
10 | * | |
11 | * Derived from net/ipv4/netfilter/ip_conntrack_proto_udp.c | |
12 | */ | |
13 | ||
14 | #include <linux/types.h> | |
15 | #include <linux/sched.h> | |
16 | #include <linux/timer.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/netfilter.h> | |
19 | #include <linux/udp.h> | |
20 | #include <linux/seq_file.h> | |
21 | #include <linux/skbuff.h> | |
22 | #include <linux/ipv6.h> | |
23 | #include <net/ip6_checksum.h> | |
24 | #include <net/checksum.h> | |
25 | #include <linux/netfilter.h> | |
26 | #include <linux/netfilter_ipv4.h> | |
27 | #include <linux/netfilter_ipv6.h> | |
28 | #include <net/netfilter/nf_conntrack_protocol.h> | |
29 | ||
babbdb1a PM |
30 | unsigned int nf_ct_udp_timeout = 30*HZ; |
31 | unsigned int nf_ct_udp_timeout_stream = 180*HZ; | |
9fb9cbb1 YK |
32 | |
33 | static int udp_pkt_to_tuple(const struct sk_buff *skb, | |
34 | unsigned int dataoff, | |
35 | struct nf_conntrack_tuple *tuple) | |
36 | { | |
37 | struct udphdr _hdr, *hp; | |
38 | ||
39 | /* Actually only need first 8 bytes. */ | |
40 | hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); | |
41 | if (hp == NULL) | |
42 | return 0; | |
43 | ||
44 | tuple->src.u.udp.port = hp->source; | |
45 | tuple->dst.u.udp.port = hp->dest; | |
46 | ||
47 | return 1; | |
48 | } | |
49 | ||
50 | static int udp_invert_tuple(struct nf_conntrack_tuple *tuple, | |
51 | const struct nf_conntrack_tuple *orig) | |
52 | { | |
53 | tuple->src.u.udp.port = orig->dst.u.udp.port; | |
54 | tuple->dst.u.udp.port = orig->src.u.udp.port; | |
55 | return 1; | |
56 | } | |
57 | ||
58 | /* Print out the per-protocol part of the tuple. */ | |
59 | static int udp_print_tuple(struct seq_file *s, | |
60 | const struct nf_conntrack_tuple *tuple) | |
61 | { | |
62 | return seq_printf(s, "sport=%hu dport=%hu ", | |
63 | ntohs(tuple->src.u.udp.port), | |
64 | ntohs(tuple->dst.u.udp.port)); | |
65 | } | |
66 | ||
67 | /* Print out the private part of the conntrack. */ | |
68 | static int udp_print_conntrack(struct seq_file *s, | |
69 | const struct nf_conn *conntrack) | |
70 | { | |
71 | return 0; | |
72 | } | |
73 | ||
74 | /* Returns verdict for packet, and may modify conntracktype */ | |
75 | static int udp_packet(struct nf_conn *conntrack, | |
76 | const struct sk_buff *skb, | |
77 | unsigned int dataoff, | |
78 | enum ip_conntrack_info ctinfo, | |
79 | int pf, | |
80 | unsigned int hooknum) | |
81 | { | |
82 | /* If we've seen traffic both ways, this is some kind of UDP | |
83 | stream. Extend timeout. */ | |
84 | if (test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) { | |
85 | nf_ct_refresh_acct(conntrack, ctinfo, skb, | |
86 | nf_ct_udp_timeout_stream); | |
87 | /* Also, more likely to be important, and not a probe */ | |
88 | if (!test_and_set_bit(IPS_ASSURED_BIT, &conntrack->status)) | |
89 | nf_conntrack_event_cache(IPCT_STATUS, skb); | |
90 | } else | |
91 | nf_ct_refresh_acct(conntrack, ctinfo, skb, nf_ct_udp_timeout); | |
92 | ||
93 | return NF_ACCEPT; | |
94 | } | |
95 | ||
96 | /* Called when a new connection for this protocol found. */ | |
97 | static int udp_new(struct nf_conn *conntrack, const struct sk_buff *skb, | |
98 | unsigned int dataoff) | |
99 | { | |
100 | return 1; | |
101 | } | |
102 | ||
103 | static int udp_error(struct sk_buff *skb, unsigned int dataoff, | |
104 | enum ip_conntrack_info *ctinfo, | |
105 | int pf, | |
96f6bf82 | 106 | unsigned int hooknum) |
9fb9cbb1 YK |
107 | { |
108 | unsigned int udplen = skb->len - dataoff; | |
109 | struct udphdr _hdr, *hdr; | |
110 | ||
111 | /* Header is too small? */ | |
112 | hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); | |
113 | if (hdr == NULL) { | |
114 | if (LOG_INVALID(IPPROTO_UDP)) | |
115 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, | |
116 | "nf_ct_udp: short packet "); | |
117 | return -NF_ACCEPT; | |
118 | } | |
119 | ||
120 | /* Truncated/malformed packets */ | |
121 | if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) { | |
122 | if (LOG_INVALID(IPPROTO_UDP)) | |
123 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, | |
124 | "nf_ct_udp: truncated/malformed packet "); | |
125 | return -NF_ACCEPT; | |
126 | } | |
127 | ||
128 | /* Packet with no checksum */ | |
129 | if (!hdr->check) | |
130 | return NF_ACCEPT; | |
131 | ||
132 | /* Checksum invalid? Ignore. | |
133 | * We skip checking packets on the outgoing path | |
84fa7933 | 134 | * because the checksum is assumed to be correct. |
9fb9cbb1 | 135 | * FIXME: Source route IP option packets --RR */ |
39a27a35 PM |
136 | if (nf_conntrack_checksum && |
137 | ((pf == PF_INET && hooknum == NF_IP_PRE_ROUTING) || | |
96f6bf82 PM |
138 | (pf == PF_INET6 && hooknum == NF_IP6_PRE_ROUTING)) && |
139 | nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) { | |
9fb9cbb1 YK |
140 | if (LOG_INVALID(IPPROTO_UDP)) |
141 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, | |
142 | "nf_ct_udp: bad UDP checksum "); | |
143 | return -NF_ACCEPT; | |
144 | } | |
145 | ||
146 | return NF_ACCEPT; | |
147 | } | |
148 | ||
9fb9cbb1 YK |
149 | struct nf_conntrack_protocol nf_conntrack_protocol_udp4 = |
150 | { | |
151 | .l3proto = PF_INET, | |
152 | .proto = IPPROTO_UDP, | |
153 | .name = "udp", | |
154 | .pkt_to_tuple = udp_pkt_to_tuple, | |
155 | .invert_tuple = udp_invert_tuple, | |
156 | .print_tuple = udp_print_tuple, | |
157 | .print_conntrack = udp_print_conntrack, | |
158 | .packet = udp_packet, | |
159 | .new = udp_new, | |
96f6bf82 | 160 | .error = udp_error, |
c1d10adb PNA |
161 | #if defined(CONFIG_NF_CT_NETLINK) || \ |
162 | defined(CONFIG_NF_CT_NETLINK_MODULE) | |
163 | .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr, | |
164 | .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple, | |
165 | #endif | |
9fb9cbb1 YK |
166 | }; |
167 | ||
168 | struct nf_conntrack_protocol nf_conntrack_protocol_udp6 = | |
169 | { | |
170 | .l3proto = PF_INET6, | |
171 | .proto = IPPROTO_UDP, | |
172 | .name = "udp", | |
173 | .pkt_to_tuple = udp_pkt_to_tuple, | |
174 | .invert_tuple = udp_invert_tuple, | |
175 | .print_tuple = udp_print_tuple, | |
176 | .print_conntrack = udp_print_conntrack, | |
177 | .packet = udp_packet, | |
178 | .new = udp_new, | |
96f6bf82 | 179 | .error = udp_error, |
c1d10adb PNA |
180 | #if defined(CONFIG_NF_CT_NETLINK) || \ |
181 | defined(CONFIG_NF_CT_NETLINK_MODULE) | |
182 | .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr, | |
183 | .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple, | |
184 | #endif | |
9fb9cbb1 YK |
185 | }; |
186 | ||
187 | EXPORT_SYMBOL(nf_conntrack_protocol_udp4); | |
188 | EXPORT_SYMBOL(nf_conntrack_protocol_udp6); |