Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ip_vs_proto_udp.c: UDP load balancing support for IPVS |
| 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
| 5 | * Julian Anastasov <ja@ssi.bg> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
Hans Schillstrom | 78b16bd | 2011-01-03 14:44:48 +0100 | [diff] [blame] | 12 | * Changes: Hans Schillstrom <hans.schillstrom@ericsson.com> |
| 13 | * Network name space (netns) aware. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
| 15 | */ |
| 16 | |
Hannes Eder | 9aada7a | 2009-07-30 14:29:44 -0700 | [diff] [blame] | 17 | #define KMSG_COMPONENT "IPVS" |
| 18 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 19 | |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 20 | #include <linux/in.h> |
| 21 | #include <linux/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/kernel.h> |
Herbert Xu | af1e1cf | 2007-10-14 00:39:33 -0700 | [diff] [blame] | 23 | #include <linux/netfilter.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/netfilter_ipv4.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 25 | #include <linux/udp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include <net/ip_vs.h> |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 28 | #include <net/ip.h> |
Stephen Rothwell | 63f2c04 | 2008-09-12 23:23:50 -0700 | [diff] [blame] | 29 | #include <net/ip6_checksum.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | static int |
Eric W. Biederman | d8f44c3 | 2015-09-21 13:02:43 -0500 | [diff] [blame] | 32 | udp_conn_schedule(struct netns_ipvs *ipvs, int af, struct sk_buff *skb, |
| 33 | struct ip_vs_proto_data *pd, |
Jesper Dangaard Brouer | d4383f0 | 2012-09-26 14:07:17 +0200 | [diff] [blame] | 34 | int *verdict, struct ip_vs_conn **cpp, |
| 35 | struct ip_vs_iphdr *iph) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
| 37 | struct ip_vs_service *svc; |
| 38 | struct udphdr _udph, *uh; |
Alex Gartrell | 2b0f39e | 2015-08-26 09:40:40 -0700 | [diff] [blame] | 39 | __be16 _ports[2], *ports = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Alex Gartrell | 2b0f39e | 2015-08-26 09:40:40 -0700 | [diff] [blame] | 41 | if (likely(!ip_vs_iph_icmp(iph))) { |
| 42 | /* IPv6 fragments, only first fragment will hit this */ |
| 43 | uh = skb_header_pointer(skb, iph->len, sizeof(_udph), &_udph); |
| 44 | if (uh) |
| 45 | ports = &uh->source; |
| 46 | } else { |
| 47 | ports = skb_header_pointer( |
| 48 | skb, iph->len, sizeof(_ports), &_ports); |
Alex Gartrell | 6044eef | 2015-08-26 09:40:37 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Alex Gartrell | 2b0f39e | 2015-08-26 09:40:40 -0700 | [diff] [blame] | 51 | if (!ports) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | *verdict = NF_DROP; |
| 53 | return 0; |
| 54 | } |
Alex Gartrell | 2b0f39e | 2015-08-26 09:40:40 -0700 | [diff] [blame] | 55 | |
Julian Anastasov | ceec4c3 | 2013-03-22 11:46:53 +0200 | [diff] [blame] | 56 | rcu_read_lock(); |
Alex Gartrell | 2b0f39e | 2015-08-26 09:40:40 -0700 | [diff] [blame] | 57 | if (likely(!ip_vs_iph_inverse(iph))) |
Eric W. Biederman | 0a4fd6c | 2015-09-21 13:01:49 -0500 | [diff] [blame] | 58 | svc = ip_vs_service_find(ipvs, af, skb->mark, iph->protocol, |
Alex Gartrell | 2b0f39e | 2015-08-26 09:40:40 -0700 | [diff] [blame] | 59 | &iph->daddr, ports[1]); |
| 60 | else |
Eric W. Biederman | 0a4fd6c | 2015-09-21 13:01:49 -0500 | [diff] [blame] | 61 | svc = ip_vs_service_find(ipvs, af, skb->mark, iph->protocol, |
Alex Gartrell | 2b0f39e | 2015-08-26 09:40:40 -0700 | [diff] [blame] | 62 | &iph->saddr, ports[0]); |
| 63 | |
Julius Volz | 3c2e050 | 2008-09-02 15:55:38 +0200 | [diff] [blame] | 64 | if (svc) { |
Julian Anastasov | 190ecd2 | 2010-10-17 16:24:37 +0300 | [diff] [blame] | 65 | int ignored; |
| 66 | |
Eric W. Biederman | d8f44c3 | 2015-09-21 13:02:43 -0500 | [diff] [blame] | 67 | if (ip_vs_todrop(ipvs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | /* |
| 69 | * It seems that we are very loaded. |
| 70 | * We have to drop this packet :( |
| 71 | */ |
Julian Anastasov | ceec4c3 | 2013-03-22 11:46:53 +0200 | [diff] [blame] | 72 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | *verdict = NF_DROP; |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Let the virtual server select a real server for the |
| 79 | * incoming connection, and create a connection entry. |
| 80 | */ |
Jesper Dangaard Brouer | d4383f0 | 2012-09-26 14:07:17 +0200 | [diff] [blame] | 81 | *cpp = ip_vs_schedule(svc, skb, pd, &ignored, iph); |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 82 | if (!*cpp && ignored <= 0) { |
| 83 | if (!ignored) |
Jesper Dangaard Brouer | d4383f0 | 2012-09-26 14:07:17 +0200 | [diff] [blame] | 84 | *verdict = ip_vs_leave(svc, skb, pd, iph); |
Julian Anastasov | ceec4c3 | 2013-03-22 11:46:53 +0200 | [diff] [blame] | 85 | else |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 86 | *verdict = NF_DROP; |
Julian Anastasov | ceec4c3 | 2013-03-22 11:46:53 +0200 | [diff] [blame] | 87 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | return 0; |
| 89 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
Julian Anastasov | ceec4c3 | 2013-03-22 11:46:53 +0200 | [diff] [blame] | 91 | rcu_read_unlock(); |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 92 | /* NF_ACCEPT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static inline void |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 98 | udp_fast_csum_update(int af, struct udphdr *uhdr, |
| 99 | const union nf_inet_addr *oldip, |
| 100 | const union nf_inet_addr *newip, |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 101 | __be16 oldport, __be16 newport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 103 | #ifdef CONFIG_IP_VS_IPV6 |
| 104 | if (af == AF_INET6) |
| 105 | uhdr->check = |
| 106 | csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6, |
| 107 | ip_vs_check_diff2(oldport, newport, |
| 108 | ~csum_unfold(uhdr->check)))); |
| 109 | else |
| 110 | #endif |
| 111 | uhdr->check = |
| 112 | csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip, |
| 113 | ip_vs_check_diff2(oldport, newport, |
| 114 | ~csum_unfold(uhdr->check)))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | if (!uhdr->check) |
Al Viro | f6ab028 | 2006-11-16 02:36:50 -0800 | [diff] [blame] | 116 | uhdr->check = CSUM_MANGLED_0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 119 | static inline void |
| 120 | udp_partial_csum_update(int af, struct udphdr *uhdr, |
| 121 | const union nf_inet_addr *oldip, |
| 122 | const union nf_inet_addr *newip, |
| 123 | __be16 oldlen, __be16 newlen) |
| 124 | { |
| 125 | #ifdef CONFIG_IP_VS_IPV6 |
| 126 | if (af == AF_INET6) |
| 127 | uhdr->check = |
Julian Anastasov | 5bc9068 | 2010-10-17 16:14:31 +0300 | [diff] [blame] | 128 | ~csum_fold(ip_vs_check_diff16(oldip->ip6, newip->ip6, |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 129 | ip_vs_check_diff2(oldlen, newlen, |
Julian Anastasov | 5bc9068 | 2010-10-17 16:14:31 +0300 | [diff] [blame] | 130 | csum_unfold(uhdr->check)))); |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 131 | else |
| 132 | #endif |
| 133 | uhdr->check = |
Julian Anastasov | 5bc9068 | 2010-10-17 16:14:31 +0300 | [diff] [blame] | 134 | ~csum_fold(ip_vs_check_diff4(oldip->ip, newip->ip, |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 135 | ip_vs_check_diff2(oldlen, newlen, |
Julian Anastasov | 5bc9068 | 2010-10-17 16:14:31 +0300 | [diff] [blame] | 136 | csum_unfold(uhdr->check)))); |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | static int |
Jesper Dangaard Brouer | d4383f0 | 2012-09-26 14:07:17 +0200 | [diff] [blame] | 141 | udp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp, |
| 142 | struct ip_vs_conn *cp, struct ip_vs_iphdr *iph) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | struct udphdr *udph; |
Jesper Dangaard Brouer | d4383f0 | 2012-09-26 14:07:17 +0200 | [diff] [blame] | 145 | unsigned int udphoff = iph->len; |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 146 | int oldlen; |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 147 | int payload_csum = 0; |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 148 | |
| 149 | #ifdef CONFIG_IP_VS_IPV6 |
Jesper Dangaard Brouer | d4383f0 | 2012-09-26 14:07:17 +0200 | [diff] [blame] | 150 | if (cp->af == AF_INET6 && iph->fragoffs) |
Jesper Dangaard Brouer | 63dca2c | 2012-09-26 14:06:41 +0200 | [diff] [blame] | 151 | return 1; |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 152 | #endif |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 153 | oldlen = skb->len - udphoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | /* csum_check requires unshared skb */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 156 | if (!skb_make_writable(skb, udphoff+sizeof(*udph))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return 0; |
| 158 | |
| 159 | if (unlikely(cp->app != NULL)) { |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 160 | int ret; |
| 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | /* Some checks before mangling */ |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 163 | if (pp->csum_check && !pp->csum_check(cp->af, skb, pp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return 0; |
| 165 | |
| 166 | /* |
| 167 | * Call application helper if needed |
| 168 | */ |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 169 | if (!(ret = ip_vs_app_pkt_out(cp, skb))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return 0; |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 171 | /* ret=2: csum update is needed after payload mangling */ |
| 172 | if (ret == 1) |
| 173 | oldlen = skb->len - udphoff; |
| 174 | else |
| 175 | payload_csum = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 178 | udph = (void *)skb_network_header(skb) + udphoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | udph->source = cp->vport; |
| 180 | |
| 181 | /* |
| 182 | * Adjust UDP checksums |
| 183 | */ |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 184 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 185 | udp_partial_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr, |
Harvey Harrison | ca62059 | 2008-11-06 23:09:56 -0800 | [diff] [blame] | 186 | htons(oldlen), |
| 187 | htons(skb->len - udphoff)); |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 188 | } else if (!payload_csum && (udph->check != 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* Only port and addr are changed, do fast csum update */ |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 190 | udp_fast_csum_update(cp->af, udph, &cp->daddr, &cp->vaddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | cp->dport, cp->vport); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 192 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 193 | skb->ip_summed = (cp->app && pp->csum_check) ? |
| 194 | CHECKSUM_UNNECESSARY : CHECKSUM_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } else { |
| 196 | /* full checksum calculation */ |
| 197 | udph->check = 0; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 198 | skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0); |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 199 | #ifdef CONFIG_IP_VS_IPV6 |
| 200 | if (cp->af == AF_INET6) |
| 201 | udph->check = csum_ipv6_magic(&cp->vaddr.in6, |
| 202 | &cp->caddr.in6, |
| 203 | skb->len - udphoff, |
| 204 | cp->protocol, skb->csum); |
| 205 | else |
| 206 | #endif |
| 207 | udph->check = csum_tcpudp_magic(cp->vaddr.ip, |
| 208 | cp->caddr.ip, |
| 209 | skb->len - udphoff, |
| 210 | cp->protocol, |
| 211 | skb->csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | if (udph->check == 0) |
Al Viro | f6ab028 | 2006-11-16 02:36:50 -0800 | [diff] [blame] | 213 | udph->check = CSUM_MANGLED_0; |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 214 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | IP_VS_DBG(11, "O-pkt: %s O-csum=%d (+%zd)\n", |
| 216 | pp->name, udph->check, |
| 217 | (char*)&(udph->check) - (char*)udph); |
| 218 | } |
| 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | static int |
Jesper Dangaard Brouer | d4383f0 | 2012-09-26 14:07:17 +0200 | [diff] [blame] | 224 | udp_dnat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp, |
| 225 | struct ip_vs_conn *cp, struct ip_vs_iphdr *iph) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
| 227 | struct udphdr *udph; |
Jesper Dangaard Brouer | d4383f0 | 2012-09-26 14:07:17 +0200 | [diff] [blame] | 228 | unsigned int udphoff = iph->len; |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 229 | int oldlen; |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 230 | int payload_csum = 0; |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 231 | |
| 232 | #ifdef CONFIG_IP_VS_IPV6 |
Jesper Dangaard Brouer | d4383f0 | 2012-09-26 14:07:17 +0200 | [diff] [blame] | 233 | if (cp->af == AF_INET6 && iph->fragoffs) |
Jesper Dangaard Brouer | 63dca2c | 2012-09-26 14:06:41 +0200 | [diff] [blame] | 234 | return 1; |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 235 | #endif |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 236 | oldlen = skb->len - udphoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
| 238 | /* csum_check requires unshared skb */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 239 | if (!skb_make_writable(skb, udphoff+sizeof(*udph))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return 0; |
| 241 | |
| 242 | if (unlikely(cp->app != NULL)) { |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 243 | int ret; |
| 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | /* Some checks before mangling */ |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 246 | if (pp->csum_check && !pp->csum_check(cp->af, skb, pp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return 0; |
| 248 | |
| 249 | /* |
| 250 | * Attempt ip_vs_app call. |
| 251 | * It will fix ip_vs_conn |
| 252 | */ |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 253 | if (!(ret = ip_vs_app_pkt_in(cp, skb))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | return 0; |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 255 | /* ret=2: csum update is needed after payload mangling */ |
| 256 | if (ret == 1) |
| 257 | oldlen = skb->len - udphoff; |
| 258 | else |
| 259 | payload_csum = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 262 | udph = (void *)skb_network_header(skb) + udphoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | udph->dest = cp->dport; |
| 264 | |
| 265 | /* |
| 266 | * Adjust UDP checksums |
| 267 | */ |
Simon Horman | 503e81f | 2008-09-08 12:04:21 +1000 | [diff] [blame] | 268 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Julian Anastasov | 5bc9068 | 2010-10-17 16:14:31 +0300 | [diff] [blame] | 269 | udp_partial_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr, |
Harvey Harrison | ca62059 | 2008-11-06 23:09:56 -0800 | [diff] [blame] | 270 | htons(oldlen), |
| 271 | htons(skb->len - udphoff)); |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 272 | } else if (!payload_csum && (udph->check != 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | /* Only port and addr are changed, do fast csum update */ |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 274 | udp_fast_csum_update(cp->af, udph, &cp->vaddr, &cp->daddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | cp->vport, cp->dport); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 276 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
Julian Anastasov | 8b27b10 | 2010-10-17 16:17:20 +0300 | [diff] [blame] | 277 | skb->ip_summed = (cp->app && pp->csum_check) ? |
| 278 | CHECKSUM_UNNECESSARY : CHECKSUM_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } else { |
| 280 | /* full checksum calculation */ |
| 281 | udph->check = 0; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 282 | skb->csum = skb_checksum(skb, udphoff, skb->len - udphoff, 0); |
Julius Volz | 0bbdd42 | 2008-09-02 15:55:42 +0200 | [diff] [blame] | 283 | #ifdef CONFIG_IP_VS_IPV6 |
| 284 | if (cp->af == AF_INET6) |
| 285 | udph->check = csum_ipv6_magic(&cp->caddr.in6, |
| 286 | &cp->daddr.in6, |
| 287 | skb->len - udphoff, |
| 288 | cp->protocol, skb->csum); |
| 289 | else |
| 290 | #endif |
| 291 | udph->check = csum_tcpudp_magic(cp->caddr.ip, |
| 292 | cp->daddr.ip, |
| 293 | skb->len - udphoff, |
| 294 | cp->protocol, |
| 295 | skb->csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | if (udph->check == 0) |
Al Viro | f6ab028 | 2006-11-16 02:36:50 -0800 | [diff] [blame] | 297 | udph->check = CSUM_MANGLED_0; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 298 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | return 1; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | static int |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 305 | udp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
| 307 | struct udphdr _udph, *uh; |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 308 | unsigned int udphoff; |
| 309 | |
| 310 | #ifdef CONFIG_IP_VS_IPV6 |
| 311 | if (af == AF_INET6) |
| 312 | udphoff = sizeof(struct ipv6hdr); |
| 313 | else |
| 314 | #endif |
| 315 | udphoff = ip_hdrlen(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | uh = skb_header_pointer(skb, udphoff, sizeof(_udph), &_udph); |
| 318 | if (uh == NULL) |
| 319 | return 0; |
| 320 | |
| 321 | if (uh->check != 0) { |
| 322 | switch (skb->ip_summed) { |
| 323 | case CHECKSUM_NONE: |
| 324 | skb->csum = skb_checksum(skb, udphoff, |
| 325 | skb->len - udphoff, 0); |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 326 | case CHECKSUM_COMPLETE: |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 327 | #ifdef CONFIG_IP_VS_IPV6 |
| 328 | if (af == AF_INET6) { |
| 329 | if (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 330 | &ipv6_hdr(skb)->daddr, |
| 331 | skb->len - udphoff, |
| 332 | ipv6_hdr(skb)->nexthdr, |
| 333 | skb->csum)) { |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 334 | IP_VS_DBG_RL_PKT(0, af, pp, skb, 0, |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 335 | "Failed checksum for"); |
| 336 | return 0; |
| 337 | } |
| 338 | } else |
| 339 | #endif |
| 340 | if (csum_tcpudp_magic(ip_hdr(skb)->saddr, |
| 341 | ip_hdr(skb)->daddr, |
| 342 | skb->len - udphoff, |
| 343 | ip_hdr(skb)->protocol, |
| 344 | skb->csum)) { |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 345 | IP_VS_DBG_RL_PKT(0, af, pp, skb, 0, |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 346 | "Failed checksum for"); |
| 347 | return 0; |
| 348 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | break; |
| 350 | default: |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 351 | /* No need to checksum. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | break; |
| 353 | } |
| 354 | } |
| 355 | return 1; |
| 356 | } |
| 357 | |
Al Viro | 75e7ce6 | 2006-11-14 21:13:28 -0800 | [diff] [blame] | 358 | static inline __u16 udp_app_hashkey(__be16 port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { |
Al Viro | 75e7ce6 | 2006-11-14 21:13:28 -0800 | [diff] [blame] | 360 | return (((__force u16)port >> UDP_APP_TAB_BITS) ^ (__force u16)port) |
| 361 | & UDP_APP_TAB_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
Eric W. Biederman | 1964891 | 2015-09-21 13:02:29 -0500 | [diff] [blame] | 365 | static int udp_register_app(struct netns_ipvs *ipvs, struct ip_vs_app *inc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | { |
| 367 | struct ip_vs_app *i; |
Al Viro | 75e7ce6 | 2006-11-14 21:13:28 -0800 | [diff] [blame] | 368 | __u16 hash; |
| 369 | __be16 port = inc->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | int ret = 0; |
Eric W. Biederman | 18d6ade | 2015-09-21 13:02:01 -0500 | [diff] [blame] | 371 | struct ip_vs_proto_data *pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | hash = udp_app_hashkey(port); |
| 374 | |
Hans Schillstrom | 78b16bd | 2011-01-03 14:44:48 +0100 | [diff] [blame] | 375 | list_for_each_entry(i, &ipvs->udp_apps[hash], p_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | if (i->port == port) { |
| 377 | ret = -EEXIST; |
| 378 | goto out; |
| 379 | } |
| 380 | } |
Julian Anastasov | 363c97d | 2013-03-21 11:58:07 +0200 | [diff] [blame] | 381 | list_add_rcu(&inc->p_list, &ipvs->udp_apps[hash]); |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 382 | atomic_inc(&pd->appcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | |
| 389 | static void |
Eric W. Biederman | 1964891 | 2015-09-21 13:02:29 -0500 | [diff] [blame] | 390 | udp_unregister_app(struct netns_ipvs *ipvs, struct ip_vs_app *inc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { |
Eric W. Biederman | 1964891 | 2015-09-21 13:02:29 -0500 | [diff] [blame] | 392 | struct ip_vs_proto_data *pd = ip_vs_proto_data_get(ipvs, IPPROTO_UDP); |
Hans Schillstrom | 78b16bd | 2011-01-03 14:44:48 +0100 | [diff] [blame] | 393 | |
Hans Schillstrom | 9bbac6a | 2011-01-03 14:44:52 +0100 | [diff] [blame] | 394 | atomic_dec(&pd->appcnt); |
Julian Anastasov | 363c97d | 2013-03-21 11:58:07 +0200 | [diff] [blame] | 395 | list_del_rcu(&inc->p_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | |
| 399 | static int udp_app_conn_bind(struct ip_vs_conn *cp) |
| 400 | { |
Eric W. Biederman | 58dbc6f | 2015-09-21 13:01:41 -0500 | [diff] [blame] | 401 | struct netns_ipvs *ipvs = cp->ipvs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | int hash; |
| 403 | struct ip_vs_app *inc; |
| 404 | int result = 0; |
| 405 | |
| 406 | /* Default binding: bind app only for NAT */ |
| 407 | if (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ) |
| 408 | return 0; |
| 409 | |
| 410 | /* Lookup application incarnations and bind the right one */ |
| 411 | hash = udp_app_hashkey(cp->vport); |
| 412 | |
Julian Anastasov | 363c97d | 2013-03-21 11:58:07 +0200 | [diff] [blame] | 413 | rcu_read_lock(); |
| 414 | list_for_each_entry_rcu(inc, &ipvs->udp_apps[hash], p_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | if (inc->port == cp->vport) { |
| 416 | if (unlikely(!ip_vs_app_inc_get(inc))) |
| 417 | break; |
Julian Anastasov | 363c97d | 2013-03-21 11:58:07 +0200 | [diff] [blame] | 418 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 420 | IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->" |
Julius Volz | cfc78c5 | 2008-09-02 15:55:53 +0200 | [diff] [blame] | 421 | "%s:%u to app %s on port %u\n", |
| 422 | __func__, |
| 423 | IP_VS_DBG_ADDR(cp->af, &cp->caddr), |
| 424 | ntohs(cp->cport), |
| 425 | IP_VS_DBG_ADDR(cp->af, &cp->vaddr), |
| 426 | ntohs(cp->vport), |
| 427 | inc->name, ntohs(inc->port)); |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | cp->app = inc; |
| 430 | if (inc->init_conn) |
| 431 | result = inc->init_conn(inc, cp); |
| 432 | goto out; |
| 433 | } |
| 434 | } |
Julian Anastasov | 363c97d | 2013-03-21 11:58:07 +0200 | [diff] [blame] | 435 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
| 437 | out: |
| 438 | return result; |
| 439 | } |
| 440 | |
| 441 | |
Hans Schillstrom | 78b16bd | 2011-01-03 14:44:48 +0100 | [diff] [blame] | 442 | static const int udp_timeouts[IP_VS_UDP_S_LAST+1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | [IP_VS_UDP_S_NORMAL] = 5*60*HZ, |
| 444 | [IP_VS_UDP_S_LAST] = 2*HZ, |
| 445 | }; |
| 446 | |
Jan Engelhardt | 36cbd3d | 2009-08-05 10:42:58 -0700 | [diff] [blame] | 447 | static const char *const udp_state_name_table[IP_VS_UDP_S_LAST+1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | [IP_VS_UDP_S_NORMAL] = "UDP", |
| 449 | [IP_VS_UDP_S_LAST] = "BUG!", |
| 450 | }; |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | static const char * udp_state_name(int state) |
| 453 | { |
| 454 | if (state >= IP_VS_UDP_S_LAST) |
| 455 | return "ERR!"; |
| 456 | return udp_state_name_table[state] ? udp_state_name_table[state] : "?"; |
| 457 | } |
| 458 | |
Simon Horman | 4a516f1 | 2011-09-16 14:11:49 +0900 | [diff] [blame] | 459 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | udp_state_transition(struct ip_vs_conn *cp, int direction, |
| 461 | const struct sk_buff *skb, |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 462 | struct ip_vs_proto_data *pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { |
Hans Schillstrom | 78b16bd | 2011-01-03 14:44:48 +0100 | [diff] [blame] | 464 | if (unlikely(!pd)) { |
| 465 | pr_err("UDP no ns data\n"); |
Simon Horman | 4a516f1 | 2011-09-16 14:11:49 +0900 | [diff] [blame] | 466 | return; |
Hans Schillstrom | 78b16bd | 2011-01-03 14:44:48 +0100 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | cp->timeout = pd->timeout_table[IP_VS_UDP_S_NORMAL]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Eric W. Biederman | 1281a9c2 | 2015-09-21 13:02:36 -0500 | [diff] [blame] | 472 | static int __udp_init(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | { |
Hans Schillstrom | 78b16bd | 2011-01-03 14:44:48 +0100 | [diff] [blame] | 474 | ip_vs_init_hash_table(ipvs->udp_apps, UDP_APP_TAB_SIZE); |
Hans Schillstrom | 78b16bd | 2011-01-03 14:44:48 +0100 | [diff] [blame] | 475 | pd->timeout_table = ip_vs_create_timeout_table((int *)udp_timeouts, |
| 476 | sizeof(udp_timeouts)); |
Hans Schillstrom | 582b8e3 | 2012-04-26 09:45:35 +0200 | [diff] [blame] | 477 | if (!pd->timeout_table) |
| 478 | return -ENOMEM; |
| 479 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Eric W. Biederman | 1281a9c2 | 2015-09-21 13:02:36 -0500 | [diff] [blame] | 482 | static void __udp_exit(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | { |
Hans Schillstrom | 78b16bd | 2011-01-03 14:44:48 +0100 | [diff] [blame] | 484 | kfree(pd->timeout_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | |
| 488 | struct ip_vs_protocol ip_vs_protocol_udp = { |
| 489 | .name = "UDP", |
| 490 | .protocol = IPPROTO_UDP, |
Julian Anastasov | 2ad17de | 2008-04-29 03:21:23 -0700 | [diff] [blame] | 491 | .num_states = IP_VS_UDP_S_LAST, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | .dont_defrag = 0, |
Hans Schillstrom | 78b16bd | 2011-01-03 14:44:48 +0100 | [diff] [blame] | 493 | .init = NULL, |
| 494 | .exit = NULL, |
| 495 | .init_netns = __udp_init, |
| 496 | .exit_netns = __udp_exit, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | .conn_schedule = udp_conn_schedule, |
Simon Horman | 5c0d237 | 2010-08-02 17:12:44 +0200 | [diff] [blame] | 498 | .conn_in_get = ip_vs_conn_in_get_proto, |
| 499 | .conn_out_get = ip_vs_conn_out_get_proto, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | .snat_handler = udp_snat_handler, |
| 501 | .dnat_handler = udp_dnat_handler, |
| 502 | .csum_check = udp_csum_check, |
| 503 | .state_transition = udp_state_transition, |
| 504 | .state_name = udp_state_name, |
| 505 | .register_app = udp_register_app, |
| 506 | .unregister_app = udp_unregister_app, |
| 507 | .app_conn_bind = udp_app_conn_bind, |
| 508 | .debug_packet = ip_vs_tcpudp_debug_packet, |
| 509 | .timeout_change = NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | }; |