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