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