Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 1 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 2 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> |
| 3 | * (C) 2007 Patrick McHardy <kaber@trash.net> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/timer.h> |
| 12 | #include <linux/module.h> |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 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> |
| 19 | |
| 20 | #include <linux/netfilter.h> |
| 21 | #include <linux/netfilter_ipv4.h> |
| 22 | #include <linux/netfilter_ipv6.h> |
| 23 | #include <net/netfilter/nf_conntrack_l4proto.h> |
| 24 | #include <net/netfilter/nf_conntrack_ecache.h> |
Patrick McHardy | f01ffbd | 2007-12-17 22:38:49 -0800 | [diff] [blame] | 25 | #include <net/netfilter/nf_log.h> |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 26 | |
| 27 | static unsigned int nf_ct_udplite_timeout __read_mostly = 30*HZ; |
| 28 | static unsigned int nf_ct_udplite_timeout_stream __read_mostly = 180*HZ; |
| 29 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 30 | static bool udplite_pkt_to_tuple(const struct sk_buff *skb, |
| 31 | unsigned int dataoff, |
| 32 | struct nf_conntrack_tuple *tuple) |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 33 | { |
Jan Engelhardt | da3f13c | 2008-01-31 04:52:29 -0800 | [diff] [blame] | 34 | const struct udphdr *hp; |
| 35 | struct udphdr _hdr; |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 36 | |
| 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; |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 40 | |
| 41 | tuple->src.u.udp.port = hp->source; |
| 42 | tuple->dst.u.udp.port = hp->dest; |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 43 | return true; |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 46 | static bool udplite_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 47 | const struct nf_conntrack_tuple *orig) |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 48 | { |
| 49 | tuple->src.u.udp.port = orig->dst.u.udp.port; |
| 50 | tuple->dst.u.udp.port = orig->src.u.udp.port; |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 51 | return true; |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* Print out the per-protocol part of the tuple. */ |
| 55 | static int udplite_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 | |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 63 | /* Returns verdict for packet, and may modify conntracktype */ |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 64 | static int udplite_packet(struct nf_conn *ct, |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 65 | const struct sk_buff *skb, |
| 66 | unsigned int dataoff, |
| 67 | enum ip_conntrack_info ctinfo, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 68 | u_int8_t pf, |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 69 | unsigned int hooknum) |
| 70 | { |
| 71 | /* If we've seen traffic both ways, this is some kind of UDP |
| 72 | stream. Extend timeout. */ |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 73 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { |
| 74 | nf_ct_refresh_acct(ct, ctinfo, skb, |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 75 | nf_ct_udplite_timeout_stream); |
| 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)) |
Alexey Dobriyan | a71996f | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 78 | nf_conntrack_event_cache(IPCT_STATUS, ct); |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 79 | } else |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 80 | nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_udplite_timeout); |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [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 udplite_new(struct nf_conn *ct, const struct sk_buff *skb, |
| 87 | unsigned int dataoff) |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 88 | { |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 89 | return true; |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Alexey Dobriyan | 74c51a1 | 2008-10-08 11:35:05 +0200 | [diff] [blame] | 92 | static int udplite_error(struct net *net, |
| 93 | struct sk_buff *skb, |
| 94 | unsigned int dataoff, |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 95 | enum ip_conntrack_info *ctinfo, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 96 | u_int8_t pf, |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 97 | unsigned int hooknum) |
| 98 | { |
| 99 | unsigned int udplen = skb->len - dataoff; |
Jan Engelhardt | da3f13c | 2008-01-31 04:52:29 -0800 | [diff] [blame] | 100 | const struct udphdr *hdr; |
| 101 | struct udphdr _hdr; |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 102 | unsigned int cscov; |
| 103 | |
| 104 | /* Header is too small? */ |
| 105 | hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); |
| 106 | if (hdr == NULL) { |
Alexey Dobriyan | c2a2c7e | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 107 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 108 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 109 | "nf_ct_udplite: short packet "); |
| 110 | return -NF_ACCEPT; |
| 111 | } |
| 112 | |
| 113 | cscov = ntohs(hdr->len); |
| 114 | if (cscov == 0) |
| 115 | cscov = udplen; |
| 116 | else if (cscov < sizeof(*hdr) || cscov > udplen) { |
Alexey Dobriyan | c2a2c7e | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 117 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 118 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 119 | "nf_ct_udplite: invalid checksum coverage "); |
| 120 | return -NF_ACCEPT; |
| 121 | } |
| 122 | |
| 123 | /* UDPLITE mandates checksums */ |
| 124 | if (!hdr->check) { |
Alexey Dobriyan | c2a2c7e | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 125 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 126 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 127 | "nf_ct_udplite: checksum missing "); |
| 128 | return -NF_ACCEPT; |
| 129 | } |
| 130 | |
| 131 | /* Checksum invalid? Ignore. */ |
Alexey Dobriyan | c04d055 | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 132 | if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING && |
Patrick McHardy | d63a650 | 2008-03-20 15:15:53 +0100 | [diff] [blame] | 133 | nf_checksum_partial(skb, hooknum, dataoff, cscov, IPPROTO_UDP, |
| 134 | pf)) { |
Alexey Dobriyan | c2a2c7e | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 135 | if (LOG_INVALID(net, IPPROTO_UDPLITE)) |
Patrick McHardy | d63a650 | 2008-03-20 15:15:53 +0100 | [diff] [blame] | 136 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 137 | "nf_ct_udplite: bad UDPLite checksum "); |
| 138 | return -NF_ACCEPT; |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | return NF_ACCEPT; |
| 142 | } |
| 143 | |
| 144 | #ifdef CONFIG_SYSCTL |
| 145 | static unsigned int udplite_sysctl_table_users; |
| 146 | static struct ctl_table_header *udplite_sysctl_header; |
| 147 | static struct ctl_table udplite_sysctl_table[] = { |
| 148 | { |
| 149 | .ctl_name = CTL_UNNUMBERED, |
| 150 | .procname = "nf_conntrack_udplite_timeout", |
| 151 | .data = &nf_ct_udplite_timeout, |
| 152 | .maxlen = sizeof(unsigned int), |
| 153 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 154 | .proc_handler = proc_dointvec_jiffies, |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 155 | }, |
| 156 | { |
| 157 | .ctl_name = CTL_UNNUMBERED, |
| 158 | .procname = "nf_conntrack_udplite_timeout_stream", |
| 159 | .data = &nf_ct_udplite_timeout_stream, |
| 160 | .maxlen = sizeof(unsigned int), |
| 161 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 162 | .proc_handler = proc_dointvec_jiffies, |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 163 | }, |
| 164 | { |
| 165 | .ctl_name = 0 |
| 166 | } |
| 167 | }; |
| 168 | #endif /* CONFIG_SYSCTL */ |
| 169 | |
| 170 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly = |
| 171 | { |
| 172 | .l3proto = PF_INET, |
| 173 | .l4proto = IPPROTO_UDPLITE, |
| 174 | .name = "udplite", |
| 175 | .pkt_to_tuple = udplite_pkt_to_tuple, |
| 176 | .invert_tuple = udplite_invert_tuple, |
| 177 | .print_tuple = udplite_print_tuple, |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 178 | .packet = udplite_packet, |
| 179 | .new = udplite_new, |
| 180 | .error = udplite_error, |
| 181 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 182 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 183 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 184 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 185 | .nla_policy = nf_ct_port_nla_policy, |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 186 | #endif |
| 187 | #ifdef CONFIG_SYSCTL |
| 188 | .ctl_table_users = &udplite_sysctl_table_users, |
| 189 | .ctl_table_header = &udplite_sysctl_header, |
| 190 | .ctl_table = udplite_sysctl_table, |
| 191 | #endif |
| 192 | }; |
| 193 | |
| 194 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly = |
| 195 | { |
| 196 | .l3proto = PF_INET6, |
| 197 | .l4proto = IPPROTO_UDPLITE, |
| 198 | .name = "udplite", |
| 199 | .pkt_to_tuple = udplite_pkt_to_tuple, |
| 200 | .invert_tuple = udplite_invert_tuple, |
| 201 | .print_tuple = udplite_print_tuple, |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 202 | .packet = udplite_packet, |
| 203 | .new = udplite_new, |
| 204 | .error = udplite_error, |
| 205 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 206 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
Patrick McHardy | 5ff4829 | 2009-04-24 15:37:44 +0200 | [diff] [blame] | 207 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 208 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 209 | .nla_policy = nf_ct_port_nla_policy, |
Patrick McHardy | 59eecdf | 2007-07-14 20:48:44 -0700 | [diff] [blame] | 210 | #endif |
| 211 | #ifdef CONFIG_SYSCTL |
| 212 | .ctl_table_users = &udplite_sysctl_table_users, |
| 213 | .ctl_table_header = &udplite_sysctl_header, |
| 214 | .ctl_table = udplite_sysctl_table, |
| 215 | #endif |
| 216 | }; |
| 217 | |
| 218 | static int __init nf_conntrack_proto_udplite_init(void) |
| 219 | { |
| 220 | int err; |
| 221 | |
| 222 | err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite4); |
| 223 | if (err < 0) |
| 224 | goto err1; |
| 225 | err = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udplite6); |
| 226 | if (err < 0) |
| 227 | goto err2; |
| 228 | return 0; |
| 229 | err2: |
| 230 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4); |
| 231 | err1: |
| 232 | return err; |
| 233 | } |
| 234 | |
| 235 | static void __exit nf_conntrack_proto_udplite_exit(void) |
| 236 | { |
| 237 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite6); |
| 238 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udplite4); |
| 239 | } |
| 240 | |
| 241 | module_init(nf_conntrack_proto_udplite_init); |
| 242 | module_exit(nf_conntrack_proto_udplite_exit); |
| 243 | |
| 244 | MODULE_LICENSE("GPL"); |