Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IPVS An implementation of the IP virtual server support for the |
| 3 | * LINUX operating system. IPVS is now implemented as a module |
| 4 | * over the Netfilter framework. IPVS can be used to build a |
| 5 | * high-performance and highly available server based on a |
| 6 | * cluster of servers. |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
| 9 | * Peter Kese <peter.kese@ijs.si> |
| 10 | * Julian Anastasov <ja@ssi.bg> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese, |
| 18 | * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms |
| 19 | * and others. |
| 20 | * |
| 21 | * Changes: |
| 22 | * Paul `Rusty' Russell properly handle non-linear skbs |
Harald Welte | 6869c4d | 2005-08-09 19:24:19 -0700 | [diff] [blame] | 23 | * Harald Welte don't use nfcache |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * |
| 25 | */ |
| 26 | |
Hannes Eder | 9aada7a | 2009-07-30 14:29:44 -0700 | [diff] [blame] | 27 | #define KMSG_COMPONENT "IPVS" |
| 28 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/module.h> |
| 31 | #include <linux/kernel.h> |
| 32 | #include <linux/ip.h> |
| 33 | #include <linux/tcp.h> |
Venkata Mohan Reddy | 2906f66 | 2010-02-18 12:31:05 +0100 | [diff] [blame] | 34 | #include <linux/sctp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/icmp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | #include <net/ip.h> |
| 39 | #include <net/tcp.h> |
| 40 | #include <net/udp.h> |
| 41 | #include <net/icmp.h> /* for icmp_send */ |
| 42 | #include <net/route.h> |
Stephen Rothwell | 2c70b51 | 2010-08-29 17:04:53 +0000 | [diff] [blame] | 43 | #include <net/ip6_checksum.h> |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 44 | #include <net/netns/generic.h> /* net_generic() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | #include <linux/netfilter.h> |
| 47 | #include <linux/netfilter_ipv4.h> |
| 48 | |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 49 | #ifdef CONFIG_IP_VS_IPV6 |
| 50 | #include <net/ipv6.h> |
| 51 | #include <linux/netfilter_ipv6.h> |
Julian Anastasov | 489fded | 2010-10-17 16:27:31 +0300 | [diff] [blame] | 52 | #include <net/ip6_route.h> |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 53 | #endif |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <net/ip_vs.h> |
| 56 | |
| 57 | |
| 58 | EXPORT_SYMBOL(register_ip_vs_scheduler); |
| 59 | EXPORT_SYMBOL(unregister_ip_vs_scheduler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | EXPORT_SYMBOL(ip_vs_proto_name); |
| 61 | EXPORT_SYMBOL(ip_vs_conn_new); |
| 62 | EXPORT_SYMBOL(ip_vs_conn_in_get); |
| 63 | EXPORT_SYMBOL(ip_vs_conn_out_get); |
| 64 | #ifdef CONFIG_IP_VS_PROTO_TCP |
| 65 | EXPORT_SYMBOL(ip_vs_tcp_conn_listen); |
| 66 | #endif |
| 67 | EXPORT_SYMBOL(ip_vs_conn_put); |
| 68 | #ifdef CONFIG_IP_VS_DEBUG |
| 69 | EXPORT_SYMBOL(ip_vs_get_debug_level); |
| 70 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 72 | int ip_vs_net_id __read_mostly; |
| 73 | #ifdef IP_VS_GENERIC_NETNS |
| 74 | EXPORT_SYMBOL(ip_vs_net_id); |
| 75 | #endif |
| 76 | /* netns cnt used for uniqueness */ |
| 77 | static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | /* ID used in ICMP lookups */ |
| 80 | #define icmp_id(icmph) (((icmph)->un).echo.id) |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 81 | #define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | const char *ip_vs_proto_name(unsigned proto) |
| 84 | { |
| 85 | static char buf[20]; |
| 86 | |
| 87 | switch (proto) { |
| 88 | case IPPROTO_IP: |
| 89 | return "IP"; |
| 90 | case IPPROTO_UDP: |
| 91 | return "UDP"; |
| 92 | case IPPROTO_TCP: |
| 93 | return "TCP"; |
Venkata Mohan Reddy | 2906f66 | 2010-02-18 12:31:05 +0100 | [diff] [blame] | 94 | case IPPROTO_SCTP: |
| 95 | return "SCTP"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | case IPPROTO_ICMP: |
| 97 | return "ICMP"; |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 98 | #ifdef CONFIG_IP_VS_IPV6 |
| 99 | case IPPROTO_ICMPV6: |
| 100 | return "ICMPv6"; |
| 101 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | default: |
| 103 | sprintf(buf, "IP_%d", proto); |
| 104 | return buf; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void ip_vs_init_hash_table(struct list_head *table, int rows) |
| 109 | { |
| 110 | while (--rows >= 0) |
| 111 | INIT_LIST_HEAD(&table[rows]); |
| 112 | } |
| 113 | |
| 114 | static inline void |
| 115 | ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb) |
| 116 | { |
| 117 | struct ip_vs_dest *dest = cp->dest; |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 118 | struct netns_ipvs *ipvs = net_ipvs(skb_net(skb)); |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) { |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 121 | struct ip_vs_cpu_stats *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 123 | s = this_cpu_ptr(dest->stats.cpustats); |
| 124 | s->ustats.inpkts++; |
| 125 | u64_stats_update_begin(&s->syncp); |
| 126 | s->ustats.inbytes += skb->len; |
| 127 | u64_stats_update_end(&s->syncp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 129 | s = this_cpu_ptr(dest->svc->stats.cpustats); |
| 130 | s->ustats.inpkts++; |
| 131 | u64_stats_update_begin(&s->syncp); |
| 132 | s->ustats.inbytes += skb->len; |
| 133 | u64_stats_update_end(&s->syncp); |
| 134 | |
| 135 | s = this_cpu_ptr(ipvs->cpustats); |
| 136 | s->ustats.inpkts++; |
| 137 | u64_stats_update_begin(&s->syncp); |
| 138 | s->ustats.inbytes += skb->len; |
| 139 | u64_stats_update_end(&s->syncp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | static inline void |
| 145 | ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb) |
| 146 | { |
| 147 | struct ip_vs_dest *dest = cp->dest; |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 148 | struct netns_ipvs *ipvs = net_ipvs(skb_net(skb)); |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) { |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 151 | struct ip_vs_cpu_stats *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 153 | s = this_cpu_ptr(dest->stats.cpustats); |
| 154 | s->ustats.outpkts++; |
| 155 | u64_stats_update_begin(&s->syncp); |
| 156 | s->ustats.outbytes += skb->len; |
| 157 | u64_stats_update_end(&s->syncp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 159 | s = this_cpu_ptr(dest->svc->stats.cpustats); |
| 160 | s->ustats.outpkts++; |
| 161 | u64_stats_update_begin(&s->syncp); |
| 162 | s->ustats.outbytes += skb->len; |
| 163 | u64_stats_update_end(&s->syncp); |
| 164 | |
| 165 | s = this_cpu_ptr(ipvs->cpustats); |
| 166 | s->ustats.outpkts++; |
| 167 | u64_stats_update_begin(&s->syncp); |
| 168 | s->ustats.outbytes += skb->len; |
| 169 | u64_stats_update_end(&s->syncp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | |
| 174 | static inline void |
| 175 | ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc) |
| 176 | { |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 177 | struct netns_ipvs *ipvs = net_ipvs(svc->net); |
| 178 | struct ip_vs_cpu_stats *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 180 | s = this_cpu_ptr(cp->dest->stats.cpustats); |
| 181 | s->ustats.conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Hans Schillstrom | b17fc99 | 2011-01-03 14:44:56 +0100 | [diff] [blame] | 183 | s = this_cpu_ptr(svc->stats.cpustats); |
| 184 | s->ustats.conns++; |
| 185 | |
| 186 | s = this_cpu_ptr(ipvs->cpustats); |
| 187 | s->ustats.conns++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | |
| 191 | static inline int |
| 192 | ip_vs_set_state(struct ip_vs_conn *cp, int direction, |
| 193 | const struct sk_buff *skb, |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 194 | struct ip_vs_proto_data *pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 196 | if (unlikely(!pd->pp->state_transition)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return 0; |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 198 | return pd->pp->state_transition(cp, direction, skb, pd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 201 | static inline int |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 202 | ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc, |
| 203 | struct sk_buff *skb, int protocol, |
| 204 | const union nf_inet_addr *caddr, __be16 cport, |
| 205 | const union nf_inet_addr *vaddr, __be16 vport, |
| 206 | struct ip_vs_conn_param *p) |
| 207 | { |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 208 | ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr, |
| 209 | vport, p); |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 210 | p->pe = svc->pe; |
| 211 | if (p->pe && p->pe->fill_param) |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 212 | return p->pe->fill_param(p, skb); |
| 213 | |
| 214 | return 0; |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* |
| 218 | * IPVS persistent scheduling function |
| 219 | * It creates a connection entry according to its template if exists, |
| 220 | * or selects a server and creates a connection entry plus a template. |
| 221 | * Locking: we are svc user (svc->refcnt), so we hold all dests too |
| 222 | * Protocols supported: TCP, UDP |
| 223 | */ |
| 224 | static struct ip_vs_conn * |
| 225 | ip_vs_sched_persist(struct ip_vs_service *svc, |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 226 | struct sk_buff *skb, |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 227 | __be16 src_port, __be16 dst_port, int *ignored) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
| 229 | struct ip_vs_conn *cp = NULL; |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 230 | struct ip_vs_iphdr iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | struct ip_vs_dest *dest; |
| 232 | struct ip_vs_conn *ct; |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 233 | __be16 dport = 0; /* destination port to forward */ |
Julian Anastasov | 3575792 | 2010-09-17 14:18:16 +0200 | [diff] [blame] | 234 | unsigned int flags; |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 235 | struct ip_vs_conn_param param; |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 236 | union nf_inet_addr snet; /* source network of the client, |
| 237 | after masking */ |
Julius Volz | cd17f9e | 2008-09-02 15:55:46 +0200 | [diff] [blame] | 238 | |
| 239 | ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | /* Mask saddr with the netmask to adjust template granularity */ |
Julius Volz | cd17f9e | 2008-09-02 15:55:46 +0200 | [diff] [blame] | 242 | #ifdef CONFIG_IP_VS_IPV6 |
| 243 | if (svc->af == AF_INET6) |
| 244 | ipv6_addr_prefix(&snet.in6, &iph.saddr.in6, svc->netmask); |
| 245 | else |
| 246 | #endif |
| 247 | snet.ip = iph.saddr.ip & svc->netmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Julius Volz | cd17f9e | 2008-09-02 15:55:46 +0200 | [diff] [blame] | 249 | IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u " |
| 250 | "mnet %s\n", |
Hans Schillstrom | ce144f2 | 2010-11-19 14:25:08 +0100 | [diff] [blame] | 251 | IP_VS_DBG_ADDR(svc->af, &iph.saddr), ntohs(src_port), |
| 252 | IP_VS_DBG_ADDR(svc->af, &iph.daddr), ntohs(dst_port), |
Julius Volz | cd17f9e | 2008-09-02 15:55:46 +0200 | [diff] [blame] | 253 | IP_VS_DBG_ADDR(svc->af, &snet)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | /* |
| 256 | * As far as we know, FTP is a very complicated network protocol, and |
| 257 | * it uses control connection and data connections. For active FTP, |
| 258 | * FTP server initialize data connection to the client, its source port |
| 259 | * is often 20. For passive FTP, FTP server tells the clients the port |
| 260 | * that it passively listens to, and the client issues the data |
| 261 | * connection. In the tunneling or direct routing mode, the load |
| 262 | * balancer is on the client-to-server half of connection, the port |
| 263 | * number is unknown to the load balancer. So, a conn template like |
| 264 | * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP |
| 265 | * service, and a template like <caddr, 0, vaddr, vport, daddr, dport> |
| 266 | * is created for other persistent services. |
| 267 | */ |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 268 | { |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 269 | int protocol = iph.protocol; |
| 270 | const union nf_inet_addr *vaddr = &iph.daddr; |
| 271 | const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) }; |
| 272 | __be16 vport = 0; |
| 273 | |
Hans Schillstrom | ce144f2 | 2010-11-19 14:25:08 +0100 | [diff] [blame] | 274 | if (dst_port == svc->port) { |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 275 | /* non-FTP template: |
| 276 | * <protocol, caddr, 0, vaddr, vport, daddr, dport> |
| 277 | * FTP template: |
| 278 | * <protocol, caddr, 0, vaddr, 0, daddr, 0> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | */ |
| 280 | if (svc->port != FTPPORT) |
Hans Schillstrom | ce144f2 | 2010-11-19 14:25:08 +0100 | [diff] [blame] | 281 | vport = dst_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } else { |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 283 | /* Note: persistent fwmark-based services and |
| 284 | * persistent port zero service are handled here. |
| 285 | * fwmark template: |
| 286 | * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0> |
| 287 | * port zero template: |
| 288 | * <protocol,caddr,0,vaddr,0,daddr,0> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | */ |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 290 | if (svc->fwmark) { |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 291 | protocol = IPPROTO_IP; |
| 292 | vaddr = &fwmark; |
| 293 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 295 | /* return *ignored = -1 so NF_DROP can be used */ |
| 296 | if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0, |
| 297 | vaddr, vport, ¶m) < 0) { |
| 298 | *ignored = -1; |
| 299 | return NULL; |
| 300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 303 | /* Check if a template already exists */ |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 304 | ct = ip_vs_ct_in_get(¶m); |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 305 | if (!ct || !ip_vs_check_template(ct)) { |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 306 | /* |
| 307 | * No template found or the dest of the connection |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 308 | * template is not available. |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 309 | * return *ignored=0 i.e. ICMP and NF_DROP |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 310 | */ |
| 311 | dest = svc->scheduler->schedule(svc, skb); |
| 312 | if (!dest) { |
| 313 | IP_VS_DBG(1, "p-schedule: no dest found.\n"); |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 314 | kfree(param.pe_data); |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 315 | *ignored = 0; |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 316 | return NULL; |
| 317 | } |
| 318 | |
Hans Schillstrom | ce144f2 | 2010-11-19 14:25:08 +0100 | [diff] [blame] | 319 | if (dst_port == svc->port && svc->port != FTPPORT) |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 320 | dport = dest->port; |
| 321 | |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 322 | /* Create a template |
| 323 | * This adds param.pe_data to the template, |
| 324 | * and thus param.pe_data will be destroyed |
| 325 | * when the template expires */ |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 326 | ct = ip_vs_conn_new(¶m, &dest->addr, dport, |
Hans Schillstrom | 0e051e6 | 2010-11-19 14:25:07 +0100 | [diff] [blame] | 327 | IP_VS_CONN_F_TEMPLATE, dest, skb->mark); |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 328 | if (ct == NULL) { |
| 329 | kfree(param.pe_data); |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 330 | *ignored = -1; |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 331 | return NULL; |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 332 | } |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 333 | |
| 334 | ct->timeout = svc->timeout; |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 335 | } else { |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 336 | /* set destination with the found template */ |
| 337 | dest = ct->dest; |
Simon Horman | 8599928 | 2010-08-22 21:37:53 +0900 | [diff] [blame] | 338 | kfree(param.pe_data); |
| 339 | } |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 340 | |
Hans Schillstrom | ce144f2 | 2010-11-19 14:25:08 +0100 | [diff] [blame] | 341 | dport = dst_port; |
Simon Horman | 5b57a98 | 2010-08-22 21:37:51 +0900 | [diff] [blame] | 342 | if (dport == svc->port && dest->port) |
| 343 | dport = dest->port; |
| 344 | |
Nick Chalk | 26ec037 | 2010-06-22 08:07:01 +0200 | [diff] [blame] | 345 | flags = (svc->flags & IP_VS_SVC_F_ONEPACKET |
| 346 | && iph.protocol == IPPROTO_UDP)? |
| 347 | IP_VS_CONN_F_ONE_PACKET : 0; |
| 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | /* |
| 350 | * Create a new connection according to the template |
| 351 | */ |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 352 | ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol, &iph.saddr, |
| 353 | src_port, &iph.daddr, dst_port, ¶m); |
Hans Schillstrom | ce144f2 | 2010-11-19 14:25:08 +0100 | [diff] [blame] | 354 | |
Hans Schillstrom | 0e051e6 | 2010-11-19 14:25:07 +0100 | [diff] [blame] | 355 | cp = ip_vs_conn_new(¶m, &dest->addr, dport, flags, dest, skb->mark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | if (cp == NULL) { |
| 357 | ip_vs_conn_put(ct); |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 358 | *ignored = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Add its control |
| 364 | */ |
| 365 | ip_vs_control_add(cp, ct); |
| 366 | ip_vs_conn_put(ct); |
| 367 | |
| 368 | ip_vs_conn_stats(cp, svc); |
| 369 | return cp; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | /* |
| 374 | * IPVS main scheduling function |
| 375 | * It selects a server according to the virtual service, and |
| 376 | * creates a connection entry. |
| 377 | * Protocols supported: TCP, UDP |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 378 | * |
| 379 | * Usage of *ignored |
| 380 | * |
| 381 | * 1 : protocol tried to schedule (eg. on SYN), found svc but the |
| 382 | * svc/scheduler decides that this packet should be accepted with |
| 383 | * NF_ACCEPT because it must not be scheduled. |
| 384 | * |
| 385 | * 0 : scheduler can not find destination, so try bypass or |
| 386 | * return ICMP and then NF_DROP (ip_vs_leave). |
| 387 | * |
| 388 | * -1 : scheduler tried to schedule but fatal error occurred, eg. |
| 389 | * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param |
| 390 | * failure such as missing Call-ID, ENOMEM on skb_linearize |
| 391 | * or pe_data. In this case we should return NF_DROP without |
| 392 | * any attempts to send ICMP with ip_vs_leave. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | */ |
| 394 | struct ip_vs_conn * |
Julian Anastasov | 190ecd2 | 2010-10-17 16:24:37 +0300 | [diff] [blame] | 395 | ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb, |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 396 | struct ip_vs_proto_data *pd, int *ignored) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | { |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 398 | struct ip_vs_protocol *pp = pd->pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | struct ip_vs_conn *cp = NULL; |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 400 | struct ip_vs_iphdr iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | struct ip_vs_dest *dest; |
Julian Anastasov | 3575792 | 2010-09-17 14:18:16 +0200 | [diff] [blame] | 402 | __be16 _ports[2], *pptr; |
| 403 | unsigned int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
Julian Anastasov | 190ecd2 | 2010-10-17 16:24:37 +0300 | [diff] [blame] | 405 | *ignored = 1; |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 406 | ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph); |
| 407 | pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | if (pptr == NULL) |
| 409 | return NULL; |
| 410 | |
| 411 | /* |
Julian Anastasov | 190ecd2 | 2010-10-17 16:24:37 +0300 | [diff] [blame] | 412 | * FTPDATA needs this check when using local real server. |
| 413 | * Never schedule Active FTPDATA connections from real server. |
| 414 | * For LVS-NAT they must be already created. For other methods |
| 415 | * with persistence the connection is created on SYN+ACK. |
| 416 | */ |
| 417 | if (pptr[0] == FTPDATA) { |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 418 | IP_VS_DBG_PKT(12, svc->af, pp, skb, 0, |
| 419 | "Not scheduling FTPDATA"); |
Julian Anastasov | 190ecd2 | 2010-10-17 16:24:37 +0300 | [diff] [blame] | 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | /* |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 424 | * Do not schedule replies from local real server. |
Julian Anastasov | 190ecd2 | 2010-10-17 16:24:37 +0300 | [diff] [blame] | 425 | */ |
| 426 | if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) && |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 427 | (cp = pp->conn_in_get(svc->af, skb, &iph, iph.len, 1))) { |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 428 | IP_VS_DBG_PKT(12, svc->af, pp, skb, 0, |
Julian Anastasov | 190ecd2 | 2010-10-17 16:24:37 +0300 | [diff] [blame] | 429 | "Not scheduling reply for existing connection"); |
| 430 | __ip_vs_conn_put(cp); |
| 431 | return NULL; |
| 432 | } |
| 433 | |
| 434 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | * Persistent service |
| 436 | */ |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 437 | if (svc->flags & IP_VS_SVC_F_PERSISTENT) |
| 438 | return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored); |
| 439 | |
| 440 | *ignored = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | /* |
| 443 | * Non-persistent service |
| 444 | */ |
| 445 | if (!svc->fwmark && pptr[1] != svc->port) { |
| 446 | if (!svc->port) |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 447 | pr_err("Schedule: port zero only supported " |
| 448 | "in persistent services, " |
| 449 | "check your ipvs configuration\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | dest = svc->scheduler->schedule(svc, skb); |
| 454 | if (dest == NULL) { |
| 455 | IP_VS_DBG(1, "Schedule: no dest found.\n"); |
| 456 | return NULL; |
| 457 | } |
| 458 | |
Nick Chalk | 26ec037 | 2010-06-22 08:07:01 +0200 | [diff] [blame] | 459 | flags = (svc->flags & IP_VS_SVC_F_ONEPACKET |
| 460 | && iph.protocol == IPPROTO_UDP)? |
| 461 | IP_VS_CONN_F_ONE_PACKET : 0; |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | /* |
| 464 | * Create a connection entry. |
| 465 | */ |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 466 | { |
| 467 | struct ip_vs_conn_param p; |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 468 | |
| 469 | ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol, |
| 470 | &iph.saddr, pptr[0], &iph.daddr, pptr[1], |
| 471 | &p); |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 472 | cp = ip_vs_conn_new(&p, &dest->addr, |
| 473 | dest->port ? dest->port : pptr[1], |
Hans Schillstrom | 0e051e6 | 2010-11-19 14:25:07 +0100 | [diff] [blame] | 474 | flags, dest, skb->mark); |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 475 | if (!cp) { |
| 476 | *ignored = -1; |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 477 | return NULL; |
Hans Schillstrom | a5959d5 | 2010-11-19 14:25:10 +0100 | [diff] [blame] | 478 | } |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 479 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Julius Volz | cd17f9e | 2008-09-02 15:55:46 +0200 | [diff] [blame] | 481 | IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u " |
| 482 | "d:%s:%u conn->flags:%X conn->refcnt:%d\n", |
| 483 | ip_vs_fwd_tag(cp), |
| 484 | IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport), |
| 485 | IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport), |
| 486 | IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport), |
| 487 | cp->flags, atomic_read(&cp->refcnt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | ip_vs_conn_stats(cp, svc); |
| 490 | return cp; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | /* |
| 495 | * Pass or drop the packet. |
| 496 | * Called by ip_vs_in, when the virtual service is available but |
| 497 | * no destination is available for a new connection. |
| 498 | */ |
| 499 | int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb, |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 500 | struct ip_vs_proto_data *pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 502 | struct netns_ipvs *ipvs; |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 503 | __be16 _ports[2], *pptr; |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 504 | struct ip_vs_iphdr iph; |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 505 | int unicast; |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 506 | |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 507 | ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
Julius Volz | 28364a5 | 2008-09-02 15:55:43 +0200 | [diff] [blame] | 509 | pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | if (pptr == NULL) { |
| 511 | ip_vs_service_put(svc); |
| 512 | return NF_DROP; |
| 513 | } |
| 514 | |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 515 | #ifdef CONFIG_IP_VS_IPV6 |
| 516 | if (svc->af == AF_INET6) |
| 517 | unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST; |
| 518 | else |
| 519 | #endif |
| 520 | unicast = (inet_addr_type(&init_net, iph.daddr.ip) == RTN_UNICAST); |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | /* if it is fwmark-based service, the cache_bypass sysctl is up |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 523 | and the destination is a non-local unicast, then create |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | a cache_bypass connection entry */ |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 525 | ipvs = net_ipvs(skb_net(skb)); |
| 526 | if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | int ret, cs; |
| 528 | struct ip_vs_conn *cp; |
Julian Anastasov | 3575792 | 2010-09-17 14:18:16 +0200 | [diff] [blame] | 529 | unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET && |
| 530 | iph.protocol == IPPROTO_UDP)? |
| 531 | IP_VS_CONN_F_ONE_PACKET : 0; |
Simon Horman | dff630d | 2008-09-17 10:10:42 +1000 | [diff] [blame] | 532 | union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
| 534 | ip_vs_service_put(svc); |
| 535 | |
| 536 | /* create a new connection entry */ |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 537 | IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__); |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 538 | { |
| 539 | struct ip_vs_conn_param p; |
Hans Schillstrom | 6e67e58 | 2011-01-03 14:44:57 +0100 | [diff] [blame] | 540 | ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol, |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 541 | &iph.saddr, pptr[0], |
| 542 | &iph.daddr, pptr[1], &p); |
| 543 | cp = ip_vs_conn_new(&p, &daddr, 0, |
| 544 | IP_VS_CONN_F_BYPASS | flags, |
Hans Schillstrom | 0e051e6 | 2010-11-19 14:25:07 +0100 | [diff] [blame] | 545 | NULL, skb->mark); |
Simon Horman | f11017e | 2010-08-22 21:37:52 +0900 | [diff] [blame] | 546 | if (!cp) |
| 547 | return NF_DROP; |
| 548 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | /* statistics */ |
| 551 | ip_vs_in_stats(cp, skb); |
| 552 | |
| 553 | /* set state */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 554 | cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | /* transmit the first SYN packet */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 557 | ret = cp->packet_xmit(skb, cp, pd->pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | /* do not touch skb anymore */ |
| 559 | |
| 560 | atomic_inc(&cp->in_pkts); |
| 561 | ip_vs_conn_put(cp); |
| 562 | return ret; |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * When the virtual ftp service is presented, packets destined |
| 567 | * for other services on the VIP may get here (except services |
| 568 | * listed in the ipvs table), pass the packets, because it is |
| 569 | * not ipvs job to decide to drop the packets. |
| 570 | */ |
| 571 | if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) { |
| 572 | ip_vs_service_put(svc); |
| 573 | return NF_ACCEPT; |
| 574 | } |
| 575 | |
| 576 | ip_vs_service_put(svc); |
| 577 | |
| 578 | /* |
| 579 | * Notify the client that the destination is unreachable, and |
| 580 | * release the socket buffer. |
| 581 | * Since it is in IP layer, the TCP socket is not actually |
| 582 | * created, the TCP RST packet cannot be sent, instead that |
| 583 | * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ |
| 584 | */ |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 585 | #ifdef CONFIG_IP_VS_IPV6 |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 586 | if (svc->af == AF_INET6) { |
| 587 | if (!skb->dev) { |
| 588 | struct net *net = dev_net(skb_dst(skb)->dev); |
| 589 | |
| 590 | skb->dev = net->loopback_dev; |
| 591 | } |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 592 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 593 | } else |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 594 | #endif |
| 595 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); |
| 596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | return NF_DROP; |
| 598 | } |
| 599 | |
Al Viro | b1550f2 | 2006-11-14 21:37:50 -0800 | [diff] [blame] | 600 | __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 602 | return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 605 | static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum) |
| 606 | { |
| 607 | if (NF_INET_LOCAL_IN == hooknum) |
| 608 | return IP_DEFRAG_VS_IN; |
| 609 | if (NF_INET_FORWARD == hooknum) |
| 610 | return IP_DEFRAG_VS_FWD; |
| 611 | return IP_DEFRAG_VS_OUT; |
| 612 | } |
| 613 | |
Herbert Xu | 776c729 | 2007-10-14 00:38:32 -0700 | [diff] [blame] | 614 | static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | { |
Herbert Xu | 776c729 | 2007-10-14 00:38:32 -0700 | [diff] [blame] | 616 | int err = ip_defrag(skb, user); |
| 617 | |
| 618 | if (!err) |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 619 | ip_send_check(ip_hdr(skb)); |
Herbert Xu | 776c729 | 2007-10-14 00:38:32 -0700 | [diff] [blame] | 620 | |
| 621 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 624 | #ifdef CONFIG_IP_VS_IPV6 |
| 625 | static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user) |
| 626 | { |
| 627 | /* TODO IPv6: Find out what to do here for IPv6 */ |
| 628 | return 0; |
| 629 | } |
| 630 | #endif |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | /* |
| 633 | * Packet has been made sufficiently writable in caller |
| 634 | * - inout: 1=in->out, 0=out->in |
| 635 | */ |
| 636 | void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp, |
| 637 | struct ip_vs_conn *cp, int inout) |
| 638 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 639 | struct iphdr *iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | unsigned int icmp_offset = iph->ihl*4; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 641 | struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) + |
| 642 | icmp_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | struct iphdr *ciph = (struct iphdr *)(icmph + 1); |
| 644 | |
| 645 | if (inout) { |
Julius Volz | e7ade46 | 2008-09-02 15:55:33 +0200 | [diff] [blame] | 646 | iph->saddr = cp->vaddr.ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | ip_send_check(iph); |
Julius Volz | e7ade46 | 2008-09-02 15:55:33 +0200 | [diff] [blame] | 648 | ciph->daddr = cp->vaddr.ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | ip_send_check(ciph); |
| 650 | } else { |
Julius Volz | e7ade46 | 2008-09-02 15:55:33 +0200 | [diff] [blame] | 651 | iph->daddr = cp->daddr.ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | ip_send_check(iph); |
Julius Volz | e7ade46 | 2008-09-02 15:55:33 +0200 | [diff] [blame] | 653 | ciph->saddr = cp->daddr.ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | ip_send_check(ciph); |
| 655 | } |
| 656 | |
Venkata Mohan Reddy | 2906f66 | 2010-02-18 12:31:05 +0100 | [diff] [blame] | 657 | /* the TCP/UDP/SCTP port */ |
| 658 | if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol || |
| 659 | IPPROTO_SCTP == ciph->protocol) { |
Al Viro | 014d730 | 2006-09-28 14:29:52 -0700 | [diff] [blame] | 660 | __be16 *ports = (void *)ciph + ciph->ihl*4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
| 662 | if (inout) |
| 663 | ports[1] = cp->vport; |
| 664 | else |
| 665 | ports[0] = cp->dport; |
| 666 | } |
| 667 | |
| 668 | /* And finally the ICMP checksum */ |
| 669 | icmph->checksum = 0; |
| 670 | icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset); |
| 671 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 672 | |
| 673 | if (inout) |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 674 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | "Forwarding altered outgoing ICMP"); |
| 676 | else |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 677 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | "Forwarding altered incoming ICMP"); |
| 679 | } |
| 680 | |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 681 | #ifdef CONFIG_IP_VS_IPV6 |
| 682 | void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp, |
| 683 | struct ip_vs_conn *cp, int inout) |
| 684 | { |
| 685 | struct ipv6hdr *iph = ipv6_hdr(skb); |
| 686 | unsigned int icmp_offset = sizeof(struct ipv6hdr); |
| 687 | struct icmp6hdr *icmph = (struct icmp6hdr *)(skb_network_header(skb) + |
| 688 | icmp_offset); |
| 689 | struct ipv6hdr *ciph = (struct ipv6hdr *)(icmph + 1); |
| 690 | |
| 691 | if (inout) { |
| 692 | iph->saddr = cp->vaddr.in6; |
| 693 | ciph->daddr = cp->vaddr.in6; |
| 694 | } else { |
| 695 | iph->daddr = cp->daddr.in6; |
| 696 | ciph->saddr = cp->daddr.in6; |
| 697 | } |
| 698 | |
Venkata Mohan Reddy | 2906f66 | 2010-02-18 12:31:05 +0100 | [diff] [blame] | 699 | /* the TCP/UDP/SCTP port */ |
| 700 | if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr || |
| 701 | IPPROTO_SCTP == ciph->nexthdr) { |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 702 | __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr); |
| 703 | |
| 704 | if (inout) |
| 705 | ports[1] = cp->vport; |
| 706 | else |
| 707 | ports[0] = cp->dport; |
| 708 | } |
| 709 | |
| 710 | /* And finally the ICMP checksum */ |
Simon Horman | 8870f84 | 2010-08-26 13:21:26 -0700 | [diff] [blame] | 711 | icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, |
| 712 | skb->len - icmp_offset, |
| 713 | IPPROTO_ICMPV6, 0); |
| 714 | skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset; |
| 715 | skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum); |
| 716 | skb->ip_summed = CHECKSUM_PARTIAL; |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 717 | |
| 718 | if (inout) |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 719 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, |
| 720 | (void *)ciph - (void *)iph, |
| 721 | "Forwarding altered outgoing ICMPv6"); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 722 | else |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 723 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, |
| 724 | (void *)ciph - (void *)iph, |
| 725 | "Forwarding altered incoming ICMPv6"); |
Julius Volz | b3cdd2a7 | 2008-09-02 15:55:45 +0200 | [diff] [blame] | 726 | } |
| 727 | #endif |
| 728 | |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 729 | /* Handle relevant response ICMP messages - forward to the right |
| 730 | * destination host. Used for NAT and local client. |
| 731 | */ |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 732 | static int handle_response_icmp(int af, struct sk_buff *skb, |
| 733 | union nf_inet_addr *snet, |
| 734 | __u8 protocol, struct ip_vs_conn *cp, |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 735 | struct ip_vs_protocol *pp, |
| 736 | unsigned int offset, unsigned int ihl) |
| 737 | { |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 738 | struct netns_ipvs *ipvs; |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 739 | unsigned int verdict = NF_DROP; |
| 740 | |
| 741 | if (IP_VS_FWD_METHOD(cp) != 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 742 | pr_err("shouldn't reach here, because the box is on the " |
| 743 | "half connection in the tun/dr module.\n"); |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* Ensure the checksum is correct */ |
| 747 | if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) { |
| 748 | /* Failed checksum! */ |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 749 | IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n", |
| 750 | IP_VS_DBG_ADDR(af, snet)); |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 751 | goto out; |
| 752 | } |
| 753 | |
Venkata Mohan Reddy | 2906f66 | 2010-02-18 12:31:05 +0100 | [diff] [blame] | 754 | if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol || |
| 755 | IPPROTO_SCTP == protocol) |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 756 | offset += 2 * sizeof(__u16); |
| 757 | if (!skb_make_writable(skb, offset)) |
| 758 | goto out; |
| 759 | |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 760 | ipvs = net_ipvs(skb_net(skb)); |
| 761 | |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 762 | #ifdef CONFIG_IP_VS_IPV6 |
| 763 | if (af == AF_INET6) |
| 764 | ip_vs_nat_icmp_v6(skb, pp, cp, 1); |
| 765 | else |
| 766 | #endif |
| 767 | ip_vs_nat_icmp(skb, pp, cp, 1); |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 768 | |
Julian Anastasov | f5a4184 | 2010-10-17 16:35:46 +0300 | [diff] [blame] | 769 | #ifdef CONFIG_IP_VS_IPV6 |
| 770 | if (af == AF_INET6) { |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 771 | if (ipvs->sysctl_snat_reroute && ip6_route_me_harder(skb) != 0) |
Julian Anastasov | f5a4184 | 2010-10-17 16:35:46 +0300 | [diff] [blame] | 772 | goto out; |
| 773 | } else |
| 774 | #endif |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 775 | if ((ipvs->sysctl_snat_reroute || |
Julian Anastasov | f5a4184 | 2010-10-17 16:35:46 +0300 | [diff] [blame] | 776 | skb_rtable(skb)->rt_flags & RTCF_LOCAL) && |
| 777 | ip_route_me_harder(skb, RTN_LOCAL) != 0) |
| 778 | goto out; |
| 779 | |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 780 | /* do the statistics and put it back */ |
| 781 | ip_vs_out_stats(cp, skb); |
| 782 | |
Julian Anastasov | cf356d6 | 2010-10-17 16:21:07 +0300 | [diff] [blame] | 783 | skb->ipvs_property = 1; |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 784 | if (!(cp->flags & IP_VS_CONN_F_NFCT)) |
Julian Anastasov | cf356d6 | 2010-10-17 16:21:07 +0300 | [diff] [blame] | 785 | ip_vs_notrack(skb); |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 786 | else |
| 787 | ip_vs_update_conntrack(skb, cp, 0); |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 788 | verdict = NF_ACCEPT; |
| 789 | |
| 790 | out: |
| 791 | __ip_vs_conn_put(cp); |
| 792 | |
| 793 | return verdict; |
| 794 | } |
| 795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | /* |
| 797 | * Handle ICMP messages in the inside-to-outside direction (outgoing). |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 798 | * Find any that might be relevant, check against existing connections. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | * Currently handles error types - unreachable, quench, ttl exceeded. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | */ |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 801 | static int ip_vs_out_icmp(struct sk_buff *skb, int *related, |
| 802 | unsigned int hooknum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | struct iphdr *iph; |
| 805 | struct icmphdr _icmph, *ic; |
| 806 | struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */ |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 807 | struct ip_vs_iphdr ciph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | struct ip_vs_conn *cp; |
| 809 | struct ip_vs_protocol *pp; |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 810 | unsigned int offset, ihl; |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 811 | union nf_inet_addr snet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | |
| 813 | *related = 1; |
| 814 | |
| 815 | /* reassemble IP fragments */ |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 816 | if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) { |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 817 | if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | return NF_STOLEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 821 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | offset = ihl = iph->ihl * 4; |
| 823 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); |
| 824 | if (ic == NULL) |
| 825 | return NF_DROP; |
| 826 | |
Harvey Harrison | 14d5e83 | 2008-10-31 00:54:29 -0700 | [diff] [blame] | 827 | IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | ic->type, ntohs(icmp_id(ic)), |
Harvey Harrison | 14d5e83 | 2008-10-31 00:54:29 -0700 | [diff] [blame] | 829 | &iph->saddr, &iph->daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
| 831 | /* |
| 832 | * Work through seeing if this is for us. |
| 833 | * These checks are supposed to be in an order that means easy |
| 834 | * things are checked first to speed up processing.... however |
| 835 | * this means that some packets will manage to get a long way |
| 836 | * down this stack and then be rejected, but that's life. |
| 837 | */ |
| 838 | if ((ic->type != ICMP_DEST_UNREACH) && |
| 839 | (ic->type != ICMP_SOURCE_QUENCH) && |
| 840 | (ic->type != ICMP_TIME_EXCEEDED)) { |
| 841 | *related = 0; |
| 842 | return NF_ACCEPT; |
| 843 | } |
| 844 | |
| 845 | /* Now find the contained IP header */ |
| 846 | offset += sizeof(_icmph); |
| 847 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); |
| 848 | if (cih == NULL) |
| 849 | return NF_ACCEPT; /* The packet looks wrong, ignore */ |
| 850 | |
| 851 | pp = ip_vs_proto_get(cih->protocol); |
| 852 | if (!pp) |
| 853 | return NF_ACCEPT; |
| 854 | |
| 855 | /* Is the embedded protocol header present? */ |
YOSHIFUJI Hideaki | 4412ec4 | 2007-03-07 14:19:10 +0900 | [diff] [blame] | 856 | if (unlikely(cih->frag_off & htons(IP_OFFSET) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | pp->dont_defrag)) |
| 858 | return NF_ACCEPT; |
| 859 | |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 860 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset, |
| 861 | "Checking outgoing ICMP for"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
| 863 | offset += cih->ihl * 4; |
| 864 | |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 865 | ip_vs_fill_iphdr(AF_INET, cih, &ciph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | /* The embedded headers contain source and dest in reverse order */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 867 | cp = pp->conn_out_get(AF_INET, skb, &ciph, offset, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | if (!cp) |
| 869 | return NF_ACCEPT; |
| 870 | |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 871 | snet.ip = iph->saddr; |
| 872 | return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp, |
| 873 | pp, offset, ihl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 876 | #ifdef CONFIG_IP_VS_IPV6 |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 877 | static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related, |
| 878 | unsigned int hooknum) |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 879 | { |
| 880 | struct ipv6hdr *iph; |
| 881 | struct icmp6hdr _icmph, *ic; |
| 882 | struct ipv6hdr _ciph, *cih; /* The ip header contained |
| 883 | within the ICMP */ |
| 884 | struct ip_vs_iphdr ciph; |
| 885 | struct ip_vs_conn *cp; |
| 886 | struct ip_vs_protocol *pp; |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 887 | unsigned int offset; |
| 888 | union nf_inet_addr snet; |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 889 | |
| 890 | *related = 1; |
| 891 | |
| 892 | /* reassemble IP fragments */ |
| 893 | if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) { |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 894 | if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum))) |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 895 | return NF_STOLEN; |
| 896 | } |
| 897 | |
| 898 | iph = ipv6_hdr(skb); |
| 899 | offset = sizeof(struct ipv6hdr); |
| 900 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); |
| 901 | if (ic == NULL) |
| 902 | return NF_DROP; |
| 903 | |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 904 | IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) %pI6->%pI6\n", |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 905 | ic->icmp6_type, ntohs(icmpv6_id(ic)), |
Harvey Harrison | 38ff4fa | 2008-10-28 16:08:13 -0700 | [diff] [blame] | 906 | &iph->saddr, &iph->daddr); |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 907 | |
| 908 | /* |
| 909 | * Work through seeing if this is for us. |
| 910 | * These checks are supposed to be in an order that means easy |
| 911 | * things are checked first to speed up processing.... however |
| 912 | * this means that some packets will manage to get a long way |
| 913 | * down this stack and then be rejected, but that's life. |
| 914 | */ |
| 915 | if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) && |
| 916 | (ic->icmp6_type != ICMPV6_PKT_TOOBIG) && |
| 917 | (ic->icmp6_type != ICMPV6_TIME_EXCEED)) { |
| 918 | *related = 0; |
| 919 | return NF_ACCEPT; |
| 920 | } |
| 921 | |
| 922 | /* Now find the contained IP header */ |
| 923 | offset += sizeof(_icmph); |
| 924 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); |
| 925 | if (cih == NULL) |
| 926 | return NF_ACCEPT; /* The packet looks wrong, ignore */ |
| 927 | |
| 928 | pp = ip_vs_proto_get(cih->nexthdr); |
| 929 | if (!pp) |
| 930 | return NF_ACCEPT; |
| 931 | |
| 932 | /* Is the embedded protocol header present? */ |
| 933 | /* TODO: we don't support fragmentation at the moment anyways */ |
| 934 | if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag)) |
| 935 | return NF_ACCEPT; |
| 936 | |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 937 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset, |
| 938 | "Checking outgoing ICMPv6 for"); |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 939 | |
| 940 | offset += sizeof(struct ipv6hdr); |
| 941 | |
| 942 | ip_vs_fill_iphdr(AF_INET6, cih, &ciph); |
| 943 | /* The embedded headers contain source and dest in reverse order */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 944 | cp = pp->conn_out_get(AF_INET6, skb, &ciph, offset, 1); |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 945 | if (!cp) |
| 946 | return NF_ACCEPT; |
| 947 | |
Simon Horman | 178f5e4 | 2008-09-08 09:34:46 +1000 | [diff] [blame] | 948 | ipv6_addr_copy(&snet.in6, &iph->saddr); |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 949 | return handle_response_icmp(AF_INET6, skb, &snet, cih->nexthdr, cp, |
| 950 | pp, offset, sizeof(struct ipv6hdr)); |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 951 | } |
| 952 | #endif |
| 953 | |
Venkata Mohan Reddy | 2906f66 | 2010-02-18 12:31:05 +0100 | [diff] [blame] | 954 | /* |
| 955 | * Check if sctp chunc is ABORT chunk |
| 956 | */ |
| 957 | static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len) |
| 958 | { |
| 959 | sctp_chunkhdr_t *sch, schunk; |
| 960 | sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t), |
| 961 | sizeof(schunk), &schunk); |
| 962 | if (sch == NULL) |
| 963 | return 0; |
| 964 | if (sch->type == SCTP_CID_ABORT) |
| 965 | return 1; |
| 966 | return 0; |
| 967 | } |
| 968 | |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 969 | static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | { |
| 971 | struct tcphdr _tcph, *th; |
| 972 | |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 973 | th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | if (th == NULL) |
| 975 | return 0; |
| 976 | return th->rst; |
| 977 | } |
| 978 | |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 979 | /* Handle response packets: rewrite addresses and send away... |
| 980 | * Used for NAT and local client. |
| 981 | */ |
| 982 | static unsigned int |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 983 | handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd, |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 984 | struct ip_vs_conn *cp, int ihl) |
| 985 | { |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 986 | struct ip_vs_protocol *pp = pd->pp; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 987 | struct netns_ipvs *ipvs; |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 988 | |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 989 | IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet"); |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 990 | |
| 991 | if (!skb_make_writable(skb, ihl)) |
| 992 | goto drop; |
| 993 | |
| 994 | /* mangle the packet */ |
| 995 | if (pp->snat_handler && !pp->snat_handler(skb, pp, cp)) |
| 996 | goto drop; |
| 997 | |
| 998 | #ifdef CONFIG_IP_VS_IPV6 |
| 999 | if (af == AF_INET6) |
| 1000 | ipv6_hdr(skb)->saddr = cp->vaddr.in6; |
| 1001 | else |
| 1002 | #endif |
| 1003 | { |
| 1004 | ip_hdr(skb)->saddr = cp->vaddr.ip; |
| 1005 | ip_send_check(ip_hdr(skb)); |
| 1006 | } |
| 1007 | |
Julian Anastasov | 8a80304 | 2010-09-21 17:38:57 +0200 | [diff] [blame] | 1008 | /* |
| 1009 | * nf_iterate does not expect change in the skb->dst->dev. |
| 1010 | * It looks like it is not fatal to enable this code for hooks |
| 1011 | * where our handlers are at the end of the chain list and |
| 1012 | * when all next handlers use skb->dst->dev and not outdev. |
| 1013 | * It will definitely route properly the inout NAT traffic |
| 1014 | * when multiple paths are used. |
| 1015 | */ |
| 1016 | |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 1017 | /* For policy routing, packets originating from this |
| 1018 | * machine itself may be routed differently to packets |
| 1019 | * passing through. We want this packet to be routed as |
| 1020 | * if it came from this machine itself. So re-compute |
| 1021 | * the routing information. |
| 1022 | */ |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 1023 | ipvs = net_ipvs(skb_net(skb)); |
| 1024 | |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 1025 | #ifdef CONFIG_IP_VS_IPV6 |
Julian Anastasov | f5a4184 | 2010-10-17 16:35:46 +0300 | [diff] [blame] | 1026 | if (af == AF_INET6) { |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 1027 | if (ipvs->sysctl_snat_reroute && ip6_route_me_harder(skb) != 0) |
Julian Anastasov | f5a4184 | 2010-10-17 16:35:46 +0300 | [diff] [blame] | 1028 | goto drop; |
| 1029 | } else |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 1030 | #endif |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 1031 | if ((ipvs->sysctl_snat_reroute || |
Julian Anastasov | f5a4184 | 2010-10-17 16:35:46 +0300 | [diff] [blame] | 1032 | skb_rtable(skb)->rt_flags & RTCF_LOCAL) && |
| 1033 | ip_route_me_harder(skb, RTN_LOCAL) != 0) |
| 1034 | goto drop; |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 1035 | |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 1036 | IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT"); |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 1037 | |
| 1038 | ip_vs_out_stats(cp, skb); |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1039 | ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd); |
Julian Anastasov | cf356d6 | 2010-10-17 16:21:07 +0300 | [diff] [blame] | 1040 | skb->ipvs_property = 1; |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 1041 | if (!(cp->flags & IP_VS_CONN_F_NFCT)) |
Julian Anastasov | cf356d6 | 2010-10-17 16:21:07 +0300 | [diff] [blame] | 1042 | ip_vs_notrack(skb); |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 1043 | else |
| 1044 | ip_vs_update_conntrack(skb, cp, 0); |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 1045 | ip_vs_conn_put(cp); |
| 1046 | |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 1047 | LeaveFunction(11); |
| 1048 | return NF_ACCEPT; |
| 1049 | |
| 1050 | drop: |
| 1051 | ip_vs_conn_put(cp); |
| 1052 | kfree_skb(skb); |
Julian Anastasov | f4bc17c | 2010-09-21 17:35:41 +0200 | [diff] [blame] | 1053 | LeaveFunction(11); |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 1054 | return NF_STOLEN; |
| 1055 | } |
| 1056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | /* |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 1058 | * Check if outgoing packet belongs to the established ip_vs_conn. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | */ |
| 1060 | static unsigned int |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1061 | ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | { |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1063 | struct net *net = NULL; |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 1064 | struct ip_vs_iphdr iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | struct ip_vs_protocol *pp; |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1066 | struct ip_vs_proto_data *pd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | struct ip_vs_conn *cp; |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 1068 | struct netns_ipvs *ipvs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
| 1070 | EnterFunction(11); |
| 1071 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1072 | /* Already marked as IPVS request or reply? */ |
Harald Welte | 6869c4d | 2005-08-09 19:24:19 -0700 | [diff] [blame] | 1073 | if (skb->ipvs_property) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | return NF_ACCEPT; |
| 1075 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1076 | /* Bad... Do not break raw sockets */ |
| 1077 | if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT && |
| 1078 | af == AF_INET)) { |
| 1079 | struct sock *sk = skb->sk; |
| 1080 | struct inet_sock *inet = inet_sk(skb->sk); |
| 1081 | |
| 1082 | if (inet && sk->sk_family == PF_INET && inet->nodefrag) |
| 1083 | return NF_ACCEPT; |
| 1084 | } |
| 1085 | |
| 1086 | if (unlikely(!skb_dst(skb))) |
| 1087 | return NF_ACCEPT; |
| 1088 | |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1089 | net = skb_net(skb); |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1090 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); |
| 1091 | #ifdef CONFIG_IP_VS_IPV6 |
| 1092 | if (af == AF_INET6) { |
| 1093 | if (unlikely(iph.protocol == IPPROTO_ICMPV6)) { |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 1094 | int related; |
| 1095 | int verdict = ip_vs_out_icmp_v6(skb, &related, |
| 1096 | hooknum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | |
Julian Anastasov | f5a4184 | 2010-10-17 16:35:46 +0300 | [diff] [blame] | 1098 | if (related) |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1099 | return verdict; |
| 1100 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); |
| 1101 | } |
| 1102 | } else |
| 1103 | #endif |
| 1104 | if (unlikely(iph.protocol == IPPROTO_ICMP)) { |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 1105 | int related; |
| 1106 | int verdict = ip_vs_out_icmp(skb, &related, hooknum); |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1107 | |
Julian Anastasov | f5a4184 | 2010-10-17 16:35:46 +0300 | [diff] [blame] | 1108 | if (related) |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1109 | return verdict; |
| 1110 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); |
| 1111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1113 | pd = ip_vs_proto_data_get(net, iph.protocol); |
| 1114 | if (unlikely(!pd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | return NF_ACCEPT; |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1116 | pp = pd->pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | |
| 1118 | /* reassemble IP fragments */ |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1119 | #ifdef CONFIG_IP_VS_IPV6 |
| 1120 | if (af == AF_INET6) { |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 1121 | if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) { |
| 1122 | if (ip_vs_gather_frags_v6(skb, |
| 1123 | ip_vs_defrag_user(hooknum))) |
| 1124 | return NF_STOLEN; |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1125 | } |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 1126 | |
| 1127 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1128 | } else |
| 1129 | #endif |
| 1130 | if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) && |
| 1131 | !pp->dont_defrag)) { |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 1132 | if (ip_vs_gather_frags(skb, |
| 1133 | ip_vs_defrag_user(hooknum))) |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1134 | return NF_STOLEN; |
| 1135 | |
| 1136 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); |
| 1137 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
| 1139 | /* |
| 1140 | * Check if the packet belongs to an existing entry |
| 1141 | */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1142 | cp = pp->conn_out_get(af, skb, &iph, iph.len, 0); |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 1143 | ipvs = net_ipvs(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1145 | if (likely(cp)) |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1146 | return handle_response(af, skb, pd, cp, iph.len); |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 1147 | if (ipvs->sysctl_nat_icmp_send && |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1148 | (pp->protocol == IPPROTO_TCP || |
| 1149 | pp->protocol == IPPROTO_UDP || |
| 1150 | pp->protocol == IPPROTO_SCTP)) { |
| 1151 | __be16 _ports[2], *pptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1153 | pptr = skb_header_pointer(skb, iph.len, |
| 1154 | sizeof(_ports), _ports); |
| 1155 | if (pptr == NULL) |
| 1156 | return NF_ACCEPT; /* Not for me */ |
Hans Schillstrom | fc72325 | 2011-01-03 14:44:43 +0100 | [diff] [blame] | 1157 | if (ip_vs_lookup_real_service(net, af, iph.protocol, |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1158 | &iph.saddr, |
| 1159 | pptr[0])) { |
| 1160 | /* |
| 1161 | * Notify the real server: there is no |
| 1162 | * existing entry if it is not RST |
| 1163 | * packet or not TCP packet. |
| 1164 | */ |
| 1165 | if ((iph.protocol != IPPROTO_TCP && |
| 1166 | iph.protocol != IPPROTO_SCTP) |
| 1167 | || ((iph.protocol == IPPROTO_TCP |
| 1168 | && !is_tcp_reset(skb, iph.len)) |
| 1169 | || (iph.protocol == IPPROTO_SCTP |
| 1170 | && !is_sctp_abort(skb, |
| 1171 | iph.len)))) { |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1172 | #ifdef CONFIG_IP_VS_IPV6 |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1173 | if (af == AF_INET6) { |
| 1174 | struct net *net = |
| 1175 | dev_net(skb_dst(skb)->dev); |
| 1176 | |
| 1177 | if (!skb->dev) |
| 1178 | skb->dev = net->loopback_dev; |
| 1179 | icmpv6_send(skb, |
| 1180 | ICMPV6_DEST_UNREACH, |
| 1181 | ICMPV6_PORT_UNREACH, |
| 1182 | 0); |
| 1183 | } else |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1184 | #endif |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1185 | icmp_send(skb, |
| 1186 | ICMP_DEST_UNREACH, |
| 1187 | ICMP_PORT_UNREACH, 0); |
| 1188 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | } |
| 1190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | } |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 1192 | IP_VS_DBG_PKT(12, af, pp, skb, 0, |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1193 | "ip_vs_out: packet continues traversal as normal"); |
| 1194 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1197 | /* |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1198 | * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain, |
| 1199 | * used only for VS/NAT. |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1200 | * Check if packet is reply for established ip_vs_conn. |
| 1201 | */ |
| 1202 | static unsigned int |
| 1203 | ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb, |
| 1204 | const struct net_device *in, const struct net_device *out, |
| 1205 | int (*okfn)(struct sk_buff *)) |
| 1206 | { |
| 1207 | return ip_vs_out(hooknum, skb, AF_INET); |
| 1208 | } |
| 1209 | |
| 1210 | /* |
| 1211 | * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT. |
| 1212 | * Check if packet is reply for established ip_vs_conn. |
| 1213 | */ |
| 1214 | static unsigned int |
| 1215 | ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb, |
| 1216 | const struct net_device *in, const struct net_device *out, |
| 1217 | int (*okfn)(struct sk_buff *)) |
| 1218 | { |
| 1219 | unsigned int verdict; |
| 1220 | |
| 1221 | /* Disable BH in LOCAL_OUT until all places are fixed */ |
| 1222 | local_bh_disable(); |
| 1223 | verdict = ip_vs_out(hooknum, skb, AF_INET); |
| 1224 | local_bh_enable(); |
| 1225 | return verdict; |
| 1226 | } |
| 1227 | |
| 1228 | #ifdef CONFIG_IP_VS_IPV6 |
| 1229 | |
| 1230 | /* |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1231 | * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain, |
| 1232 | * used only for VS/NAT. |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1233 | * Check if packet is reply for established ip_vs_conn. |
| 1234 | */ |
| 1235 | static unsigned int |
| 1236 | ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb, |
| 1237 | const struct net_device *in, const struct net_device *out, |
| 1238 | int (*okfn)(struct sk_buff *)) |
| 1239 | { |
| 1240 | return ip_vs_out(hooknum, skb, AF_INET6); |
| 1241 | } |
| 1242 | |
| 1243 | /* |
| 1244 | * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT. |
| 1245 | * Check if packet is reply for established ip_vs_conn. |
| 1246 | */ |
| 1247 | static unsigned int |
| 1248 | ip_vs_local_reply6(unsigned int hooknum, struct sk_buff *skb, |
| 1249 | const struct net_device *in, const struct net_device *out, |
| 1250 | int (*okfn)(struct sk_buff *)) |
| 1251 | { |
| 1252 | unsigned int verdict; |
| 1253 | |
| 1254 | /* Disable BH in LOCAL_OUT until all places are fixed */ |
| 1255 | local_bh_disable(); |
| 1256 | verdict = ip_vs_out(hooknum, skb, AF_INET6); |
| 1257 | local_bh_enable(); |
| 1258 | return verdict; |
| 1259 | } |
| 1260 | |
| 1261 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
| 1263 | /* |
| 1264 | * Handle ICMP messages in the outside-to-inside direction (incoming). |
| 1265 | * Find any that might be relevant, check against existing connections, |
| 1266 | * forward to the right destination host if relevant. |
| 1267 | * Currently handles error types - unreachable, quench, ttl exceeded. |
| 1268 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1269 | static int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 1270 | ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | { |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1272 | struct net *net = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | struct iphdr *iph; |
| 1274 | struct icmphdr _icmph, *ic; |
| 1275 | struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */ |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 1276 | struct ip_vs_iphdr ciph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | struct ip_vs_conn *cp; |
| 1278 | struct ip_vs_protocol *pp; |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1279 | struct ip_vs_proto_data *pd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | unsigned int offset, ihl, verdict; |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 1281 | union nf_inet_addr snet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | |
| 1283 | *related = 1; |
| 1284 | |
| 1285 | /* reassemble IP fragments */ |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1286 | if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) { |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 1287 | if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | return NF_STOLEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1291 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | offset = ihl = iph->ihl * 4; |
| 1293 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); |
| 1294 | if (ic == NULL) |
| 1295 | return NF_DROP; |
| 1296 | |
Harvey Harrison | 14d5e83 | 2008-10-31 00:54:29 -0700 | [diff] [blame] | 1297 | IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | ic->type, ntohs(icmp_id(ic)), |
Harvey Harrison | 14d5e83 | 2008-10-31 00:54:29 -0700 | [diff] [blame] | 1299 | &iph->saddr, &iph->daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
| 1301 | /* |
| 1302 | * Work through seeing if this is for us. |
| 1303 | * These checks are supposed to be in an order that means easy |
| 1304 | * things are checked first to speed up processing.... however |
| 1305 | * this means that some packets will manage to get a long way |
| 1306 | * down this stack and then be rejected, but that's life. |
| 1307 | */ |
| 1308 | if ((ic->type != ICMP_DEST_UNREACH) && |
| 1309 | (ic->type != ICMP_SOURCE_QUENCH) && |
| 1310 | (ic->type != ICMP_TIME_EXCEEDED)) { |
| 1311 | *related = 0; |
| 1312 | return NF_ACCEPT; |
| 1313 | } |
| 1314 | |
| 1315 | /* Now find the contained IP header */ |
| 1316 | offset += sizeof(_icmph); |
| 1317 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); |
| 1318 | if (cih == NULL) |
| 1319 | return NF_ACCEPT; /* The packet looks wrong, ignore */ |
| 1320 | |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1321 | net = skb_net(skb); |
| 1322 | pd = ip_vs_proto_data_get(net, cih->protocol); |
| 1323 | if (!pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | return NF_ACCEPT; |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1325 | pp = pd->pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | |
| 1327 | /* Is the embedded protocol header present? */ |
YOSHIFUJI Hideaki | 4412ec4 | 2007-03-07 14:19:10 +0900 | [diff] [blame] | 1328 | if (unlikely(cih->frag_off & htons(IP_OFFSET) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | pp->dont_defrag)) |
| 1330 | return NF_ACCEPT; |
| 1331 | |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 1332 | IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset, |
| 1333 | "Checking incoming ICMP for"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | |
| 1335 | offset += cih->ihl * 4; |
| 1336 | |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 1337 | ip_vs_fill_iphdr(AF_INET, cih, &ciph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | /* The embedded headers contain source and dest in reverse order */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1339 | cp = pp->conn_in_get(AF_INET, skb, &ciph, offset, 1); |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 1340 | if (!cp) { |
| 1341 | /* The packet could also belong to a local client */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1342 | cp = pp->conn_out_get(AF_INET, skb, &ciph, offset, 1); |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 1343 | if (cp) { |
| 1344 | snet.ip = iph->saddr; |
| 1345 | return handle_response_icmp(AF_INET, skb, &snet, |
| 1346 | cih->protocol, cp, pp, |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 1347 | offset, ihl); |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 1348 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | return NF_ACCEPT; |
Malcolm Turnbull | 4856c84 | 2008-09-05 11:17:13 +1000 | [diff] [blame] | 1350 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | |
| 1352 | verdict = NF_DROP; |
| 1353 | |
| 1354 | /* Ensure the checksum is correct */ |
Herbert Xu | 6047637 | 2007-04-09 11:59:39 -0700 | [diff] [blame] | 1355 | if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | /* Failed checksum! */ |
Harvey Harrison | 14d5e83 | 2008-10-31 00:54:29 -0700 | [diff] [blame] | 1357 | IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n", |
| 1358 | &iph->saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | goto out; |
| 1360 | } |
| 1361 | |
| 1362 | /* do the statistics and put it back */ |
| 1363 | ip_vs_in_stats(cp, skb); |
| 1364 | if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol) |
| 1365 | offset += 2 * sizeof(__u16); |
| 1366 | verdict = ip_vs_icmp_xmit(skb, cp, pp, offset); |
Julian Anastasov | 489fded | 2010-10-17 16:27:31 +0300 | [diff] [blame] | 1367 | /* LOCALNODE from FORWARD hook is not supported */ |
| 1368 | if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD && |
| 1369 | skb_rtable(skb)->rt_flags & RTCF_LOCAL) { |
| 1370 | IP_VS_DBG(1, "%s(): " |
| 1371 | "local delivery to %pI4 but in FORWARD\n", |
| 1372 | __func__, &skb_rtable(skb)->rt_dst); |
| 1373 | verdict = NF_DROP; |
| 1374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | |
| 1376 | out: |
| 1377 | __ip_vs_conn_put(cp); |
| 1378 | |
| 1379 | return verdict; |
| 1380 | } |
| 1381 | |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1382 | #ifdef CONFIG_IP_VS_IPV6 |
| 1383 | static int |
| 1384 | ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum) |
| 1385 | { |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1386 | struct net *net = NULL; |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1387 | struct ipv6hdr *iph; |
| 1388 | struct icmp6hdr _icmph, *ic; |
| 1389 | struct ipv6hdr _ciph, *cih; /* The ip header contained |
| 1390 | within the ICMP */ |
| 1391 | struct ip_vs_iphdr ciph; |
| 1392 | struct ip_vs_conn *cp; |
| 1393 | struct ip_vs_protocol *pp; |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1394 | struct ip_vs_proto_data *pd; |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1395 | unsigned int offset, verdict; |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 1396 | union nf_inet_addr snet; |
Julian Anastasov | 489fded | 2010-10-17 16:27:31 +0300 | [diff] [blame] | 1397 | struct rt6_info *rt; |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1398 | |
| 1399 | *related = 1; |
| 1400 | |
| 1401 | /* reassemble IP fragments */ |
| 1402 | if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) { |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 1403 | if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum))) |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1404 | return NF_STOLEN; |
| 1405 | } |
| 1406 | |
| 1407 | iph = ipv6_hdr(skb); |
| 1408 | offset = sizeof(struct ipv6hdr); |
| 1409 | ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph); |
| 1410 | if (ic == NULL) |
| 1411 | return NF_DROP; |
| 1412 | |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 1413 | IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) %pI6->%pI6\n", |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1414 | ic->icmp6_type, ntohs(icmpv6_id(ic)), |
Harvey Harrison | 38ff4fa | 2008-10-28 16:08:13 -0700 | [diff] [blame] | 1415 | &iph->saddr, &iph->daddr); |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1416 | |
| 1417 | /* |
| 1418 | * Work through seeing if this is for us. |
| 1419 | * These checks are supposed to be in an order that means easy |
| 1420 | * things are checked first to speed up processing.... however |
| 1421 | * this means that some packets will manage to get a long way |
| 1422 | * down this stack and then be rejected, but that's life. |
| 1423 | */ |
| 1424 | if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) && |
| 1425 | (ic->icmp6_type != ICMPV6_PKT_TOOBIG) && |
| 1426 | (ic->icmp6_type != ICMPV6_TIME_EXCEED)) { |
| 1427 | *related = 0; |
| 1428 | return NF_ACCEPT; |
| 1429 | } |
| 1430 | |
| 1431 | /* Now find the contained IP header */ |
| 1432 | offset += sizeof(_icmph); |
| 1433 | cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph); |
| 1434 | if (cih == NULL) |
| 1435 | return NF_ACCEPT; /* The packet looks wrong, ignore */ |
| 1436 | |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1437 | net = skb_net(skb); |
| 1438 | pd = ip_vs_proto_data_get(net, cih->nexthdr); |
| 1439 | if (!pd) |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1440 | return NF_ACCEPT; |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1441 | pp = pd->pp; |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1442 | |
| 1443 | /* Is the embedded protocol header present? */ |
| 1444 | /* TODO: we don't support fragmentation at the moment anyways */ |
| 1445 | if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag)) |
| 1446 | return NF_ACCEPT; |
| 1447 | |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 1448 | IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset, |
| 1449 | "Checking incoming ICMPv6 for"); |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1450 | |
| 1451 | offset += sizeof(struct ipv6hdr); |
| 1452 | |
| 1453 | ip_vs_fill_iphdr(AF_INET6, cih, &ciph); |
| 1454 | /* The embedded headers contain source and dest in reverse order */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1455 | cp = pp->conn_in_get(AF_INET6, skb, &ciph, offset, 1); |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 1456 | if (!cp) { |
| 1457 | /* The packet could also belong to a local client */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1458 | cp = pp->conn_out_get(AF_INET6, skb, &ciph, offset, 1); |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 1459 | if (cp) { |
Simon Horman | 178f5e4 | 2008-09-08 09:34:46 +1000 | [diff] [blame] | 1460 | ipv6_addr_copy(&snet.in6, &iph->saddr); |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 1461 | return handle_response_icmp(AF_INET6, skb, &snet, |
| 1462 | cih->nexthdr, |
| 1463 | cp, pp, offset, |
| 1464 | sizeof(struct ipv6hdr)); |
| 1465 | } |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1466 | return NF_ACCEPT; |
Simon Horman | f2428ed | 2008-09-05 11:17:14 +1000 | [diff] [blame] | 1467 | } |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1468 | |
| 1469 | verdict = NF_DROP; |
| 1470 | |
| 1471 | /* do the statistics and put it back */ |
| 1472 | ip_vs_in_stats(cp, skb); |
Venkata Mohan Reddy | 2906f66 | 2010-02-18 12:31:05 +0100 | [diff] [blame] | 1473 | if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr || |
| 1474 | IPPROTO_SCTP == cih->nexthdr) |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1475 | offset += 2 * sizeof(__u16); |
| 1476 | verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset); |
Julian Anastasov | 489fded | 2010-10-17 16:27:31 +0300 | [diff] [blame] | 1477 | /* LOCALNODE from FORWARD hook is not supported */ |
| 1478 | if (verdict == NF_ACCEPT && hooknum == NF_INET_FORWARD && |
| 1479 | (rt = (struct rt6_info *) skb_dst(skb)) && |
| 1480 | rt->rt6i_dev && rt->rt6i_dev->flags & IFF_LOOPBACK) { |
| 1481 | IP_VS_DBG(1, "%s(): " |
| 1482 | "local delivery to %pI6 but in FORWARD\n", |
| 1483 | __func__, &rt->rt6i_dst); |
| 1484 | verdict = NF_DROP; |
| 1485 | } |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1486 | |
| 1487 | __ip_vs_conn_put(cp); |
| 1488 | |
| 1489 | return verdict; |
| 1490 | } |
| 1491 | #endif |
| 1492 | |
| 1493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | /* |
| 1495 | * Check if it's for virtual services, look it up, |
| 1496 | * and send it on its way... |
| 1497 | */ |
| 1498 | static unsigned int |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1499 | ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1501 | struct net *net; |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 1502 | struct ip_vs_iphdr iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | struct ip_vs_protocol *pp; |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1504 | struct ip_vs_proto_data *pd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | struct ip_vs_conn *cp; |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1506 | int ret, restart, pkts; |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1507 | struct netns_ipvs *ipvs; |
Julius Volz | 51ef348 | 2008-09-02 15:55:40 +0200 | [diff] [blame] | 1508 | |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1509 | /* Already marked as IPVS request or reply? */ |
| 1510 | if (skb->ipvs_property) |
| 1511 | return NF_ACCEPT; |
| 1512 | |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1513 | /* |
| 1514 | * Big tappo: |
| 1515 | * - remote client: only PACKET_HOST |
| 1516 | * - route: used for struct net when skb->dev is unset |
| 1517 | */ |
| 1518 | if (unlikely((skb->pkt_type != PACKET_HOST && |
| 1519 | hooknum != NF_INET_LOCAL_OUT) || |
| 1520 | !skb_dst(skb))) { |
| 1521 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); |
| 1522 | IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s" |
| 1523 | " ignored in hook %u\n", |
| 1524 | skb->pkt_type, iph.protocol, |
| 1525 | IP_VS_DBG_ADDR(af, &iph.daddr), hooknum); |
| 1526 | return NF_ACCEPT; |
| 1527 | } |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1528 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1530 | /* Bad... Do not break raw sockets */ |
| 1531 | if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT && |
| 1532 | af == AF_INET)) { |
| 1533 | struct sock *sk = skb->sk; |
| 1534 | struct inet_sock *inet = inet_sk(skb->sk); |
| 1535 | |
| 1536 | if (inet && sk->sk_family == PF_INET && inet->nodefrag) |
| 1537 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
Julius Volz | 94b2655 | 2009-08-31 16:22:23 +0200 | [diff] [blame] | 1540 | #ifdef CONFIG_IP_VS_IPV6 |
| 1541 | if (af == AF_INET6) { |
| 1542 | if (unlikely(iph.protocol == IPPROTO_ICMPV6)) { |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 1543 | int related; |
| 1544 | int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | |
Julius Volz | 94b2655 | 2009-08-31 16:22:23 +0200 | [diff] [blame] | 1546 | if (related) |
| 1547 | return verdict; |
| 1548 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); |
| 1549 | } |
| 1550 | } else |
| 1551 | #endif |
| 1552 | if (unlikely(iph.protocol == IPPROTO_ICMP)) { |
Julian Anastasov | 1ca5bb5 | 2010-10-17 16:32:29 +0300 | [diff] [blame] | 1553 | int related; |
| 1554 | int verdict = ip_vs_in_icmp(skb, &related, hooknum); |
Julius Volz | 94b2655 | 2009-08-31 16:22:23 +0200 | [diff] [blame] | 1555 | |
| 1556 | if (related) |
| 1557 | return verdict; |
| 1558 | ip_vs_fill_iphdr(af, skb_network_header(skb), &iph); |
| 1559 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1561 | net = skb_net(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | /* Protocol supported? */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1563 | pd = ip_vs_proto_data_get(net, iph.protocol); |
| 1564 | if (unlikely(!pd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | return NF_ACCEPT; |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1566 | pp = pd->pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | /* |
| 1568 | * Check if the packet belongs to an existing connection entry |
| 1569 | */ |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1570 | cp = pp->conn_in_get(af, skb, &iph, iph.len, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | |
| 1572 | if (unlikely(!cp)) { |
| 1573 | int v; |
| 1574 | |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1575 | if (!pp->conn_schedule(af, skb, pd, &v, &cp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | return v; |
| 1577 | } |
| 1578 | |
| 1579 | if (unlikely(!cp)) { |
| 1580 | /* sorry, all this trouble for a no-hit :) */ |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 1581 | IP_VS_DBG_PKT(12, af, pp, skb, 0, |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1582 | "ip_vs_in: packet continues traversal as normal"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | return NF_ACCEPT; |
| 1584 | } |
| 1585 | |
Julian Anastasov | 0d79641 | 2010-10-17 16:46:17 +0300 | [diff] [blame] | 1586 | IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet"); |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1587 | net = skb_net(skb); |
| 1588 | ipvs = net_ipvs(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | /* Check the server status */ |
| 1590 | if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) { |
| 1591 | /* the destination server is not available */ |
| 1592 | |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 1593 | if (ipvs->sysctl_expire_nodest_conn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | /* try to expire the connection immediately */ |
| 1595 | ip_vs_conn_expire_now(cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | } |
Julian Anastasov | dc8103f | 2005-11-08 09:40:05 -0800 | [diff] [blame] | 1597 | /* don't restart its timer, and silently |
| 1598 | drop the packet. */ |
| 1599 | __ip_vs_conn_put(cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | return NF_DROP; |
| 1601 | } |
| 1602 | |
| 1603 | ip_vs_in_stats(cp, skb); |
Hans Schillstrom | 9330419 | 2011-01-03 14:44:51 +0100 | [diff] [blame] | 1604 | restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | if (cp->packet_xmit) |
| 1606 | ret = cp->packet_xmit(skb, cp, pp); |
| 1607 | /* do not touch skb anymore */ |
| 1608 | else { |
| 1609 | IP_VS_DBG_RL("warning: packet_xmit is null"); |
| 1610 | ret = NF_ACCEPT; |
| 1611 | } |
| 1612 | |
Rumen G. Bogdanovski | efac527 | 2007-11-07 02:36:55 -0800 | [diff] [blame] | 1613 | /* Increase its packet counter and check if it is needed |
| 1614 | * to be synchronized |
| 1615 | * |
| 1616 | * Sync connection if it is about to close to |
| 1617 | * encorage the standby servers to update the connections timeout |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 1618 | * |
| 1619 | * For ONE_PKT let ip_vs_sync_conn() do the filter work. |
Rumen G. Bogdanovski | efac527 | 2007-11-07 02:36:55 -0800 | [diff] [blame] | 1620 | */ |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1621 | |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 1622 | if (cp->flags & IP_VS_CONN_F_ONE_PACKET) |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 1623 | pkts = ipvs->sysctl_sync_threshold[0]; |
Hans Schillstrom | 986a075 | 2010-11-19 14:25:13 +0100 | [diff] [blame] | 1624 | else |
| 1625 | pkts = atomic_add_return(1, &cp->in_pkts); |
| 1626 | |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1627 | if ((ipvs->sync_state & IP_VS_STATE_MASTER) && |
Venkata Mohan Reddy | 2906f66 | 2010-02-18 12:31:05 +0100 | [diff] [blame] | 1628 | cp->protocol == IPPROTO_SCTP) { |
| 1629 | if ((cp->state == IP_VS_SCTP_S_ESTABLISHED && |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 1630 | (pkts % ipvs->sysctl_sync_threshold[1] |
| 1631 | == ipvs->sysctl_sync_threshold[0])) || |
Venkata Mohan Reddy | 2906f66 | 2010-02-18 12:31:05 +0100 | [diff] [blame] | 1632 | (cp->old_state != cp->state && |
| 1633 | ((cp->state == IP_VS_SCTP_S_CLOSED) || |
| 1634 | (cp->state == IP_VS_SCTP_S_SHUT_ACK_CLI) || |
| 1635 | (cp->state == IP_VS_SCTP_S_SHUT_ACK_SER)))) { |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1636 | ip_vs_sync_conn(net, cp); |
Venkata Mohan Reddy | 2906f66 | 2010-02-18 12:31:05 +0100 | [diff] [blame] | 1637 | goto out; |
| 1638 | } |
| 1639 | } |
| 1640 | |
Julian Anastasov | 8ed2163 | 2010-09-01 22:19:14 +0000 | [diff] [blame] | 1641 | /* Keep this block last: TCP and others with pp->num_states <= 1 */ |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1642 | else if ((ipvs->sync_state & IP_VS_STATE_MASTER) && |
Rumen G. Bogdanovski | efac527 | 2007-11-07 02:36:55 -0800 | [diff] [blame] | 1643 | (((cp->protocol != IPPROTO_TCP || |
| 1644 | cp->state == IP_VS_TCP_S_ESTABLISHED) && |
Hans Schillstrom | a0840e2 | 2011-01-03 14:44:58 +0100 | [diff] [blame^] | 1645 | (pkts % ipvs->sysctl_sync_threshold[1] |
| 1646 | == ipvs->sysctl_sync_threshold[0])) || |
Rumen G. Bogdanovski | efac527 | 2007-11-07 02:36:55 -0800 | [diff] [blame] | 1647 | ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) && |
| 1648 | ((cp->state == IP_VS_TCP_S_FIN_WAIT) || |
Xiaotian Feng | 9abfe31 | 2009-12-14 16:38:21 +0100 | [diff] [blame] | 1649 | (cp->state == IP_VS_TCP_S_CLOSE) || |
Rumen G. Bogdanovski | 9d3a0de | 2008-07-16 20:04:23 -0700 | [diff] [blame] | 1650 | (cp->state == IP_VS_TCP_S_CLOSE_WAIT) || |
| 1651 | (cp->state == IP_VS_TCP_S_TIME_WAIT))))) |
Hans Schillstrom | f131315 | 2011-01-03 14:44:55 +0100 | [diff] [blame] | 1652 | ip_vs_sync_conn(net, cp); |
Venkata Mohan Reddy | 2906f66 | 2010-02-18 12:31:05 +0100 | [diff] [blame] | 1653 | out: |
Rumen G. Bogdanovski | efac527 | 2007-11-07 02:36:55 -0800 | [diff] [blame] | 1654 | cp->old_state = cp->state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | |
| 1656 | ip_vs_conn_put(cp); |
| 1657 | return ret; |
| 1658 | } |
| 1659 | |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1660 | /* |
| 1661 | * AF_INET handler in NF_INET_LOCAL_IN chain |
| 1662 | * Schedule and forward packets from remote clients |
| 1663 | */ |
| 1664 | static unsigned int |
| 1665 | ip_vs_remote_request4(unsigned int hooknum, struct sk_buff *skb, |
| 1666 | const struct net_device *in, |
| 1667 | const struct net_device *out, |
| 1668 | int (*okfn)(struct sk_buff *)) |
| 1669 | { |
| 1670 | return ip_vs_in(hooknum, skb, AF_INET); |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | * AF_INET handler in NF_INET_LOCAL_OUT chain |
| 1675 | * Schedule and forward packets from local clients |
| 1676 | */ |
| 1677 | static unsigned int |
| 1678 | ip_vs_local_request4(unsigned int hooknum, struct sk_buff *skb, |
| 1679 | const struct net_device *in, const struct net_device *out, |
| 1680 | int (*okfn)(struct sk_buff *)) |
| 1681 | { |
| 1682 | unsigned int verdict; |
| 1683 | |
| 1684 | /* Disable BH in LOCAL_OUT until all places are fixed */ |
| 1685 | local_bh_disable(); |
| 1686 | verdict = ip_vs_in(hooknum, skb, AF_INET); |
| 1687 | local_bh_enable(); |
| 1688 | return verdict; |
| 1689 | } |
| 1690 | |
| 1691 | #ifdef CONFIG_IP_VS_IPV6 |
| 1692 | |
| 1693 | /* |
| 1694 | * AF_INET6 handler in NF_INET_LOCAL_IN chain |
| 1695 | * Schedule and forward packets from remote clients |
| 1696 | */ |
| 1697 | static unsigned int |
| 1698 | ip_vs_remote_request6(unsigned int hooknum, struct sk_buff *skb, |
| 1699 | const struct net_device *in, |
| 1700 | const struct net_device *out, |
| 1701 | int (*okfn)(struct sk_buff *)) |
| 1702 | { |
| 1703 | return ip_vs_in(hooknum, skb, AF_INET6); |
| 1704 | } |
| 1705 | |
| 1706 | /* |
| 1707 | * AF_INET6 handler in NF_INET_LOCAL_OUT chain |
| 1708 | * Schedule and forward packets from local clients |
| 1709 | */ |
| 1710 | static unsigned int |
| 1711 | ip_vs_local_request6(unsigned int hooknum, struct sk_buff *skb, |
| 1712 | const struct net_device *in, const struct net_device *out, |
| 1713 | int (*okfn)(struct sk_buff *)) |
| 1714 | { |
| 1715 | unsigned int verdict; |
| 1716 | |
| 1717 | /* Disable BH in LOCAL_OUT until all places are fixed */ |
| 1718 | local_bh_disable(); |
| 1719 | verdict = ip_vs_in(hooknum, skb, AF_INET6); |
| 1720 | local_bh_enable(); |
| 1721 | return verdict; |
| 1722 | } |
| 1723 | |
| 1724 | #endif |
| 1725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | |
| 1727 | /* |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 1728 | * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | * related packets destined for 0.0.0.0/0. |
| 1730 | * When fwmark-based virtual service is used, such as transparent |
| 1731 | * cache cluster, TCP packets can be marked and routed to ip_vs_in, |
| 1732 | * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 1733 | * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | * and send them to ip_vs_in_icmp. |
| 1735 | */ |
| 1736 | static unsigned int |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 1737 | ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | const struct net_device *in, const struct net_device *out, |
| 1739 | int (*okfn)(struct sk_buff *)) |
| 1740 | { |
| 1741 | int r; |
| 1742 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 1743 | if (ip_hdr(skb)->protocol != IPPROTO_ICMP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | return NF_ACCEPT; |
| 1745 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 1746 | return ip_vs_in_icmp(skb, &r, hooknum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
Julius Volz | 2a3b791 | 2008-09-02 15:55:47 +0200 | [diff] [blame] | 1749 | #ifdef CONFIG_IP_VS_IPV6 |
| 1750 | static unsigned int |
| 1751 | ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb, |
| 1752 | const struct net_device *in, const struct net_device *out, |
| 1753 | int (*okfn)(struct sk_buff *)) |
| 1754 | { |
| 1755 | int r; |
| 1756 | |
| 1757 | if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6) |
| 1758 | return NF_ACCEPT; |
| 1759 | |
| 1760 | return ip_vs_in_icmp_v6(skb, &r, hooknum); |
| 1761 | } |
| 1762 | #endif |
| 1763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | |
Patrick McHardy | 1999414 | 2007-12-05 01:23:00 -0800 | [diff] [blame] | 1765 | static struct nf_hook_ops ip_vs_ops[] __read_mostly = { |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1766 | /* After packet filtering, change source only for VS/NAT */ |
| 1767 | { |
| 1768 | .hook = ip_vs_reply4, |
| 1769 | .owner = THIS_MODULE, |
| 1770 | .pf = PF_INET, |
| 1771 | .hooknum = NF_INET_LOCAL_IN, |
| 1772 | .priority = 99, |
| 1773 | }, |
Patrick McHardy | 41c5b31 | 2007-12-05 01:22:43 -0800 | [diff] [blame] | 1774 | /* After packet filtering, forward packet through VS/DR, VS/TUN, |
| 1775 | * or VS/NAT(change destination), so that filtering rules can be |
| 1776 | * applied to IPVS. */ |
| 1777 | { |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1778 | .hook = ip_vs_remote_request4, |
Patrick McHardy | 41c5b31 | 2007-12-05 01:22:43 -0800 | [diff] [blame] | 1779 | .owner = THIS_MODULE, |
| 1780 | .pf = PF_INET, |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1781 | .hooknum = NF_INET_LOCAL_IN, |
| 1782 | .priority = 101, |
Patrick McHardy | 41c5b31 | 2007-12-05 01:22:43 -0800 | [diff] [blame] | 1783 | }, |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1784 | /* Before ip_vs_in, change source only for VS/NAT */ |
Patrick McHardy | 41c5b31 | 2007-12-05 01:22:43 -0800 | [diff] [blame] | 1785 | { |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1786 | .hook = ip_vs_local_reply4, |
Patrick McHardy | 41c5b31 | 2007-12-05 01:22:43 -0800 | [diff] [blame] | 1787 | .owner = THIS_MODULE, |
| 1788 | .pf = PF_INET, |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1789 | .hooknum = NF_INET_LOCAL_OUT, |
| 1790 | .priority = -99, |
Patrick McHardy | 41c5b31 | 2007-12-05 01:22:43 -0800 | [diff] [blame] | 1791 | }, |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1792 | /* After mangle, schedule and forward local requests */ |
| 1793 | { |
| 1794 | .hook = ip_vs_local_request4, |
| 1795 | .owner = THIS_MODULE, |
| 1796 | .pf = PF_INET, |
| 1797 | .hooknum = NF_INET_LOCAL_OUT, |
| 1798 | .priority = -98, |
| 1799 | }, |
Patrick McHardy | 41c5b31 | 2007-12-05 01:22:43 -0800 | [diff] [blame] | 1800 | /* After packet filtering (but before ip_vs_out_icmp), catch icmp |
| 1801 | * destined for 0.0.0.0/0, which is for incoming IPVS connections */ |
| 1802 | { |
| 1803 | .hook = ip_vs_forward_icmp, |
| 1804 | .owner = THIS_MODULE, |
| 1805 | .pf = PF_INET, |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1806 | .hooknum = NF_INET_FORWARD, |
| 1807 | .priority = 99, |
Patrick McHardy | 41c5b31 | 2007-12-05 01:22:43 -0800 | [diff] [blame] | 1808 | }, |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1809 | /* After packet filtering, change source only for VS/NAT */ |
| 1810 | { |
| 1811 | .hook = ip_vs_reply4, |
| 1812 | .owner = THIS_MODULE, |
| 1813 | .pf = PF_INET, |
| 1814 | .hooknum = NF_INET_FORWARD, |
| 1815 | .priority = 100, |
| 1816 | }, |
Julius Volz | 473b23d | 2008-09-02 15:55:54 +0200 | [diff] [blame] | 1817 | #ifdef CONFIG_IP_VS_IPV6 |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1818 | /* After packet filtering, change source only for VS/NAT */ |
| 1819 | { |
| 1820 | .hook = ip_vs_reply6, |
| 1821 | .owner = THIS_MODULE, |
| 1822 | .pf = PF_INET6, |
| 1823 | .hooknum = NF_INET_LOCAL_IN, |
| 1824 | .priority = 99, |
| 1825 | }, |
Julius Volz | 473b23d | 2008-09-02 15:55:54 +0200 | [diff] [blame] | 1826 | /* After packet filtering, forward packet through VS/DR, VS/TUN, |
| 1827 | * or VS/NAT(change destination), so that filtering rules can be |
| 1828 | * applied to IPVS. */ |
| 1829 | { |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1830 | .hook = ip_vs_remote_request6, |
Julius Volz | 473b23d | 2008-09-02 15:55:54 +0200 | [diff] [blame] | 1831 | .owner = THIS_MODULE, |
| 1832 | .pf = PF_INET6, |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1833 | .hooknum = NF_INET_LOCAL_IN, |
| 1834 | .priority = 101, |
Julius Volz | 473b23d | 2008-09-02 15:55:54 +0200 | [diff] [blame] | 1835 | }, |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1836 | /* Before ip_vs_in, change source only for VS/NAT */ |
Julius Volz | 473b23d | 2008-09-02 15:55:54 +0200 | [diff] [blame] | 1837 | { |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1838 | .hook = ip_vs_local_reply6, |
Julius Volz | 473b23d | 2008-09-02 15:55:54 +0200 | [diff] [blame] | 1839 | .owner = THIS_MODULE, |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1840 | .pf = PF_INET, |
| 1841 | .hooknum = NF_INET_LOCAL_OUT, |
| 1842 | .priority = -99, |
Julius Volz | 473b23d | 2008-09-02 15:55:54 +0200 | [diff] [blame] | 1843 | }, |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1844 | /* After mangle, schedule and forward local requests */ |
| 1845 | { |
| 1846 | .hook = ip_vs_local_request6, |
| 1847 | .owner = THIS_MODULE, |
| 1848 | .pf = PF_INET6, |
| 1849 | .hooknum = NF_INET_LOCAL_OUT, |
| 1850 | .priority = -98, |
| 1851 | }, |
Julius Volz | 473b23d | 2008-09-02 15:55:54 +0200 | [diff] [blame] | 1852 | /* After packet filtering (but before ip_vs_out_icmp), catch icmp |
| 1853 | * destined for 0.0.0.0/0, which is for incoming IPVS connections */ |
| 1854 | { |
| 1855 | .hook = ip_vs_forward_icmp_v6, |
| 1856 | .owner = THIS_MODULE, |
| 1857 | .pf = PF_INET6, |
Julian Anastasov | cb59155 | 2010-10-17 16:40:51 +0300 | [diff] [blame] | 1858 | .hooknum = NF_INET_FORWARD, |
| 1859 | .priority = 99, |
Julius Volz | 473b23d | 2008-09-02 15:55:54 +0200 | [diff] [blame] | 1860 | }, |
Julian Anastasov | fc60476 | 2010-10-17 16:38:15 +0300 | [diff] [blame] | 1861 | /* After packet filtering, change source only for VS/NAT */ |
| 1862 | { |
| 1863 | .hook = ip_vs_reply6, |
| 1864 | .owner = THIS_MODULE, |
| 1865 | .pf = PF_INET6, |
| 1866 | .hooknum = NF_INET_FORWARD, |
| 1867 | .priority = 100, |
| 1868 | }, |
Julius Volz | 473b23d | 2008-09-02 15:55:54 +0200 | [diff] [blame] | 1869 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | }; |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1871 | /* |
| 1872 | * Initialize IP Virtual Server netns mem. |
| 1873 | */ |
| 1874 | static int __net_init __ip_vs_init(struct net *net) |
| 1875 | { |
| 1876 | struct netns_ipvs *ipvs; |
| 1877 | |
| 1878 | if (!net_eq(net, &init_net)) { |
| 1879 | pr_err("The final patch for enabling netns is missing\n"); |
| 1880 | return -EPERM; |
| 1881 | } |
| 1882 | ipvs = net_generic(net, ip_vs_net_id); |
| 1883 | if (ipvs == NULL) { |
| 1884 | pr_err("%s(): no memory.\n", __func__); |
| 1885 | return -ENOMEM; |
| 1886 | } |
| 1887 | /* Counters used for creating unique names */ |
| 1888 | ipvs->gen = atomic_read(&ipvs_netns_cnt); |
| 1889 | atomic_inc(&ipvs_netns_cnt); |
| 1890 | net->ipvs = ipvs; |
| 1891 | printk(KERN_INFO "IPVS: Creating netns size=%lu id=%d\n", |
| 1892 | sizeof(struct netns_ipvs), ipvs->gen); |
| 1893 | return 0; |
| 1894 | } |
| 1895 | |
| 1896 | static void __net_exit __ip_vs_cleanup(struct net *net) |
| 1897 | { |
| 1898 | struct netns_ipvs *ipvs = net_ipvs(net); |
| 1899 | |
| 1900 | IP_VS_DBG(10, "ipvs netns %d released\n", ipvs->gen); |
| 1901 | } |
| 1902 | |
| 1903 | static struct pernet_operations ipvs_core_ops = { |
| 1904 | .init = __ip_vs_init, |
| 1905 | .exit = __ip_vs_cleanup, |
| 1906 | .id = &ip_vs_net_id, |
| 1907 | .size = sizeof(struct netns_ipvs), |
| 1908 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | |
| 1910 | /* |
| 1911 | * Initialize IP Virtual Server |
| 1912 | */ |
| 1913 | static int __init ip_vs_init(void) |
| 1914 | { |
| 1915 | int ret; |
| 1916 | |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1917 | ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */ |
| 1918 | if (ret < 0) |
| 1919 | return ret; |
Sven Wegener | a919cf4 | 2008-08-14 00:47:16 +0200 | [diff] [blame] | 1920 | |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1921 | ip_vs_estimator_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | ret = ip_vs_control_init(); |
| 1923 | if (ret < 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1924 | pr_err("can't setup control.\n"); |
Sven Wegener | a919cf4 | 2008-08-14 00:47:16 +0200 | [diff] [blame] | 1925 | goto cleanup_estimator; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | ip_vs_protocol_init(); |
| 1929 | |
| 1930 | ret = ip_vs_app_init(); |
| 1931 | if (ret < 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1932 | pr_err("can't setup application helper.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | goto cleanup_protocol; |
| 1934 | } |
| 1935 | |
| 1936 | ret = ip_vs_conn_init(); |
| 1937 | if (ret < 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1938 | pr_err("can't setup connection table.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | goto cleanup_app; |
| 1940 | } |
| 1941 | |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1942 | ret = ip_vs_sync_init(); |
| 1943 | if (ret < 0) { |
| 1944 | pr_err("can't setup sync data.\n"); |
| 1945 | goto cleanup_conn; |
| 1946 | } |
| 1947 | |
Patrick McHardy | 41c5b31 | 2007-12-05 01:22:43 -0800 | [diff] [blame] | 1948 | ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | if (ret < 0) { |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1950 | pr_err("can't register hooks.\n"); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1951 | goto cleanup_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1954 | pr_info("ipvs loaded.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | return ret; |
| 1956 | |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1957 | cleanup_sync: |
| 1958 | ip_vs_sync_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | cleanup_conn: |
| 1960 | ip_vs_conn_cleanup(); |
| 1961 | cleanup_app: |
| 1962 | ip_vs_app_cleanup(); |
| 1963 | cleanup_protocol: |
| 1964 | ip_vs_protocol_cleanup(); |
| 1965 | ip_vs_control_cleanup(); |
Sven Wegener | a919cf4 | 2008-08-14 00:47:16 +0200 | [diff] [blame] | 1966 | cleanup_estimator: |
| 1967 | ip_vs_estimator_cleanup(); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1968 | unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | return ret; |
| 1970 | } |
| 1971 | |
| 1972 | static void __exit ip_vs_cleanup(void) |
| 1973 | { |
Patrick McHardy | 41c5b31 | 2007-12-05 01:22:43 -0800 | [diff] [blame] | 1974 | nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops)); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1975 | ip_vs_sync_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | ip_vs_conn_cleanup(); |
| 1977 | ip_vs_app_cleanup(); |
| 1978 | ip_vs_protocol_cleanup(); |
| 1979 | ip_vs_control_cleanup(); |
Sven Wegener | a919cf4 | 2008-08-14 00:47:16 +0200 | [diff] [blame] | 1980 | ip_vs_estimator_cleanup(); |
Hans Schillstrom | 61b1ab4 | 2011-01-03 14:44:42 +0100 | [diff] [blame] | 1981 | unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */ |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 1982 | pr_info("ipvs unloaded.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | module_init(ip_vs_init); |
| 1986 | module_exit(ip_vs_cleanup); |
| 1987 | MODULE_LICENSE("GPL"); |