Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 25 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 26 | #include <linux/netfilter.h> |
| 27 | #include <linux/netfilter_ipv4.h> |
| 28 | #include <linux/netfilter_ipv6.h> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 29 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 30 | #include <net/netfilter/nf_conntrack_ecache.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 31 | |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 32 | static unsigned int nf_ct_udp_timeout __read_mostly = 30*HZ; |
| 33 | static unsigned int nf_ct_udp_timeout_stream __read_mostly = 180*HZ; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 34 | |
| 35 | static int udp_pkt_to_tuple(const struct sk_buff *skb, |
| 36 | unsigned int dataoff, |
| 37 | struct nf_conntrack_tuple *tuple) |
| 38 | { |
| 39 | struct udphdr _hdr, *hp; |
| 40 | |
| 41 | /* Actually only need first 8 bytes. */ |
| 42 | hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); |
| 43 | if (hp == NULL) |
| 44 | return 0; |
| 45 | |
| 46 | tuple->src.u.udp.port = hp->source; |
| 47 | tuple->dst.u.udp.port = hp->dest; |
| 48 | |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | static int udp_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 53 | const struct nf_conntrack_tuple *orig) |
| 54 | { |
| 55 | tuple->src.u.udp.port = orig->dst.u.udp.port; |
| 56 | tuple->dst.u.udp.port = orig->src.u.udp.port; |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | /* Print out the per-protocol part of the tuple. */ |
| 61 | static int udp_print_tuple(struct seq_file *s, |
| 62 | const struct nf_conntrack_tuple *tuple) |
| 63 | { |
| 64 | return seq_printf(s, "sport=%hu dport=%hu ", |
| 65 | ntohs(tuple->src.u.udp.port), |
| 66 | ntohs(tuple->dst.u.udp.port)); |
| 67 | } |
| 68 | |
| 69 | /* Print out the private part of the conntrack. */ |
| 70 | static int udp_print_conntrack(struct seq_file *s, |
| 71 | const struct nf_conn *conntrack) |
| 72 | { |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* Returns verdict for packet, and may modify conntracktype */ |
| 77 | static int udp_packet(struct nf_conn *conntrack, |
| 78 | const struct sk_buff *skb, |
| 79 | unsigned int dataoff, |
| 80 | enum ip_conntrack_info ctinfo, |
| 81 | int pf, |
| 82 | unsigned int hooknum) |
| 83 | { |
| 84 | /* If we've seen traffic both ways, this is some kind of UDP |
| 85 | stream. Extend timeout. */ |
| 86 | if (test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) { |
| 87 | nf_ct_refresh_acct(conntrack, ctinfo, skb, |
| 88 | nf_ct_udp_timeout_stream); |
| 89 | /* Also, more likely to be important, and not a probe */ |
| 90 | if (!test_and_set_bit(IPS_ASSURED_BIT, &conntrack->status)) |
| 91 | nf_conntrack_event_cache(IPCT_STATUS, skb); |
| 92 | } else |
| 93 | nf_ct_refresh_acct(conntrack, ctinfo, skb, nf_ct_udp_timeout); |
| 94 | |
| 95 | return NF_ACCEPT; |
| 96 | } |
| 97 | |
| 98 | /* Called when a new connection for this protocol found. */ |
| 99 | static int udp_new(struct nf_conn *conntrack, const struct sk_buff *skb, |
| 100 | unsigned int dataoff) |
| 101 | { |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | static int udp_error(struct sk_buff *skb, unsigned int dataoff, |
| 106 | enum ip_conntrack_info *ctinfo, |
| 107 | int pf, |
Patrick McHardy | 96f6bf8 | 2006-04-06 14:19:24 -0700 | [diff] [blame] | 108 | unsigned int hooknum) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 109 | { |
| 110 | unsigned int udplen = skb->len - dataoff; |
| 111 | struct udphdr _hdr, *hdr; |
| 112 | |
| 113 | /* Header is too small? */ |
| 114 | hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); |
| 115 | if (hdr == NULL) { |
| 116 | if (LOG_INVALID(IPPROTO_UDP)) |
| 117 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 118 | "nf_ct_udp: short packet "); |
| 119 | return -NF_ACCEPT; |
| 120 | } |
| 121 | |
| 122 | /* Truncated/malformed packets */ |
| 123 | if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) { |
| 124 | if (LOG_INVALID(IPPROTO_UDP)) |
| 125 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 126 | "nf_ct_udp: truncated/malformed packet "); |
| 127 | return -NF_ACCEPT; |
| 128 | } |
| 129 | |
| 130 | /* Packet with no checksum */ |
| 131 | if (!hdr->check) |
| 132 | return NF_ACCEPT; |
| 133 | |
| 134 | /* Checksum invalid? Ignore. |
| 135 | * We skip checking packets on the outgoing path |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 136 | * because the checksum is assumed to be correct. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 137 | * FIXME: Source route IP option packets --RR */ |
Patrick McHardy | 39a27a3 | 2006-05-29 18:23:54 -0700 | [diff] [blame] | 138 | if (nf_conntrack_checksum && |
| 139 | ((pf == PF_INET && hooknum == NF_IP_PRE_ROUTING) || |
Patrick McHardy | 96f6bf8 | 2006-04-06 14:19:24 -0700 | [diff] [blame] | 140 | (pf == PF_INET6 && hooknum == NF_IP6_PRE_ROUTING)) && |
| 141 | nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 142 | if (LOG_INVALID(IPPROTO_UDP)) |
| 143 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 144 | "nf_ct_udp: bad UDP checksum "); |
| 145 | return -NF_ACCEPT; |
| 146 | } |
| 147 | |
| 148 | return NF_ACCEPT; |
| 149 | } |
| 150 | |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 151 | #ifdef CONFIG_SYSCTL |
| 152 | static unsigned int udp_sysctl_table_users; |
| 153 | static struct ctl_table_header *udp_sysctl_header; |
| 154 | static struct ctl_table udp_sysctl_table[] = { |
| 155 | { |
| 156 | .ctl_name = NET_NF_CONNTRACK_UDP_TIMEOUT, |
| 157 | .procname = "nf_conntrack_udp_timeout", |
| 158 | .data = &nf_ct_udp_timeout, |
| 159 | .maxlen = sizeof(unsigned int), |
| 160 | .mode = 0644, |
| 161 | .proc_handler = &proc_dointvec_jiffies, |
| 162 | }, |
| 163 | { |
| 164 | .ctl_name = NET_NF_CONNTRACK_UDP_TIMEOUT_STREAM, |
| 165 | .procname = "nf_conntrack_udp_timeout_stream", |
| 166 | .data = &nf_ct_udp_timeout_stream, |
| 167 | .maxlen = sizeof(unsigned int), |
| 168 | .mode = 0644, |
| 169 | .proc_handler = &proc_dointvec_jiffies, |
| 170 | }, |
| 171 | { |
| 172 | .ctl_name = 0 |
| 173 | } |
| 174 | }; |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame^] | 175 | #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT |
| 176 | static struct ctl_table udp_compat_sysctl_table[] = { |
| 177 | { |
| 178 | .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT, |
| 179 | .procname = "ip_conntrack_udp_timeout", |
| 180 | .data = &nf_ct_udp_timeout, |
| 181 | .maxlen = sizeof(unsigned int), |
| 182 | .mode = 0644, |
| 183 | .proc_handler = &proc_dointvec_jiffies, |
| 184 | }, |
| 185 | { |
| 186 | .ctl_name = NET_IPV4_NF_CONNTRACK_UDP_TIMEOUT_STREAM, |
| 187 | .procname = "ip_conntrack_udp_timeout_stream", |
| 188 | .data = &nf_ct_udp_timeout_stream, |
| 189 | .maxlen = sizeof(unsigned int), |
| 190 | .mode = 0644, |
| 191 | .proc_handler = &proc_dointvec_jiffies, |
| 192 | }, |
| 193 | { |
| 194 | .ctl_name = 0 |
| 195 | } |
| 196 | }; |
| 197 | #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */ |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 198 | #endif /* CONFIG_SYSCTL */ |
| 199 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 200 | struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 = |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 201 | { |
| 202 | .l3proto = PF_INET, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 203 | .l4proto = IPPROTO_UDP, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 204 | .name = "udp", |
| 205 | .pkt_to_tuple = udp_pkt_to_tuple, |
| 206 | .invert_tuple = udp_invert_tuple, |
| 207 | .print_tuple = udp_print_tuple, |
| 208 | .print_conntrack = udp_print_conntrack, |
| 209 | .packet = udp_packet, |
| 210 | .new = udp_new, |
Patrick McHardy | 96f6bf8 | 2006-04-06 14:19:24 -0700 | [diff] [blame] | 211 | .error = udp_error, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 212 | #if defined(CONFIG_NF_CT_NETLINK) || \ |
| 213 | defined(CONFIG_NF_CT_NETLINK_MODULE) |
| 214 | .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr, |
| 215 | .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple, |
| 216 | #endif |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 217 | #ifdef CONFIG_SYSCTL |
| 218 | .ctl_table_users = &udp_sysctl_table_users, |
| 219 | .ctl_table_header = &udp_sysctl_header, |
| 220 | .ctl_table = udp_sysctl_table, |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame^] | 221 | #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT |
| 222 | .ctl_compat_table = udp_compat_sysctl_table, |
| 223 | #endif |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 224 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 225 | }; |
| 226 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 227 | struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 = |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 228 | { |
| 229 | .l3proto = PF_INET6, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 230 | .l4proto = IPPROTO_UDP, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 231 | .name = "udp", |
| 232 | .pkt_to_tuple = udp_pkt_to_tuple, |
| 233 | .invert_tuple = udp_invert_tuple, |
| 234 | .print_tuple = udp_print_tuple, |
| 235 | .print_conntrack = udp_print_conntrack, |
| 236 | .packet = udp_packet, |
| 237 | .new = udp_new, |
Patrick McHardy | 96f6bf8 | 2006-04-06 14:19:24 -0700 | [diff] [blame] | 238 | .error = udp_error, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 239 | #if defined(CONFIG_NF_CT_NETLINK) || \ |
| 240 | defined(CONFIG_NF_CT_NETLINK_MODULE) |
| 241 | .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr, |
| 242 | .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple, |
| 243 | #endif |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 244 | #ifdef CONFIG_SYSCTL |
| 245 | .ctl_table_users = &udp_sysctl_table_users, |
| 246 | .ctl_table_header = &udp_sysctl_header, |
| 247 | .ctl_table = udp_sysctl_table, |
| 248 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 249 | }; |
| 250 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 251 | EXPORT_SYMBOL(nf_conntrack_l4proto_udp4); |
| 252 | EXPORT_SYMBOL(nf_conntrack_l4proto_udp6); |