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