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