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