blob: 38fbc194b9cb72835c497439713d23f16d99ca8c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -07008 * 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 Welte6869c4d2005-08-09 19:24:19 -070023 * Harald Welte don't use nfcache
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 *
25 */
26
Hannes Eder9aada7a2009-07-30 14:29:44 -070027#define KMSG_COMPONENT "IPVS"
28#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/ip.h>
33#include <linux/tcp.h>
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +010034#include <linux/sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/icmp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
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 Rothwell2c70b512010-08-29 17:04:53 +000043#include <net/ip6_checksum.h>
Hans Schillstrom61b1ab42011-01-03 14:44:42 +010044#include <net/netns/generic.h> /* net_generic() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <linux/netfilter.h>
47#include <linux/netfilter_ipv4.h>
48
Julius Volz2a3b7912008-09-02 15:55:47 +020049#ifdef CONFIG_IP_VS_IPV6
50#include <net/ipv6.h>
51#include <linux/netfilter_ipv6.h>
Julian Anastasov489fded2010-10-17 16:27:31 +030052#include <net/ip6_route.h>
Julius Volz2a3b7912008-09-02 15:55:47 +020053#endif
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/ip_vs.h>
56
57
58EXPORT_SYMBOL(register_ip_vs_scheduler);
59EXPORT_SYMBOL(unregister_ip_vs_scheduler);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060EXPORT_SYMBOL(ip_vs_proto_name);
61EXPORT_SYMBOL(ip_vs_conn_new);
62EXPORT_SYMBOL(ip_vs_conn_in_get);
63EXPORT_SYMBOL(ip_vs_conn_out_get);
64#ifdef CONFIG_IP_VS_PROTO_TCP
65EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
66#endif
67EXPORT_SYMBOL(ip_vs_conn_put);
68#ifdef CONFIG_IP_VS_DEBUG
69EXPORT_SYMBOL(ip_vs_get_debug_level);
70#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Julian Anastasovb962abd2013-03-09 23:25:08 +020072static int ip_vs_net_id __read_mostly;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +010073/* netns cnt used for uniqueness */
74static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/* ID used in ICMP lookups */
77#define icmp_id(icmph) (((icmph)->un).echo.id)
Julius Volz2a3b7912008-09-02 15:55:47 +020078#define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Eric Dumazet95c96172012-04-15 05:58:06 +000080const char *ip_vs_proto_name(unsigned int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
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 Reddy2906f662010-02-18 12:31:05 +010091 case IPPROTO_SCTP:
92 return "SCTP";
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 case IPPROTO_ICMP:
94 return "ICMP";
Julius Volz2a3b7912008-09-02 15:55:47 +020095#ifdef CONFIG_IP_VS_IPV6
96 case IPPROTO_ICMPV6:
97 return "ICMPv6";
98#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 default:
Masanari Iida1404c3a2014-04-21 09:16:47 +0900100 sprintf(buf, "IP_%u", proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return buf;
102 }
103}
104
105void ip_vs_init_hash_table(struct list_head *table, int rows)
106{
107 while (--rows >= 0)
108 INIT_LIST_HEAD(&table[rows]);
109}
110
111static inline void
112ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
113{
114 struct ip_vs_dest *dest = cp->dest;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100115 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
Hans Schillstromb17fc992011-01-03 14:44:56 +0100118 struct ip_vs_cpu_stats *s;
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300119 struct ip_vs_service *svc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Hans Schillstromb17fc992011-01-03 14:44:56 +0100121 s = this_cpu_ptr(dest->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100122 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200123 s->cnt.inpkts++;
124 s->cnt.inbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100125 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300127 rcu_read_lock();
128 svc = rcu_dereference(dest->svc);
129 s = this_cpu_ptr(svc->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100130 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200131 s->cnt.inpkts++;
132 s->cnt.inbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100133 u64_stats_update_end(&s->syncp);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300134 rcu_read_unlock();
Hans Schillstromb17fc992011-01-03 14:44:56 +0100135
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200136 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100137 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200138 s->cnt.inpkts++;
139 s->cnt.inbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100140 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142}
143
144
145static inline void
146ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
147{
148 struct ip_vs_dest *dest = cp->dest;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100149 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
Hans Schillstromb17fc992011-01-03 14:44:56 +0100152 struct ip_vs_cpu_stats *s;
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300153 struct ip_vs_service *svc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Hans Schillstromb17fc992011-01-03 14:44:56 +0100155 s = this_cpu_ptr(dest->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100156 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200157 s->cnt.outpkts++;
158 s->cnt.outbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100159 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300161 rcu_read_lock();
162 svc = rcu_dereference(dest->svc);
163 s = this_cpu_ptr(svc->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100164 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200165 s->cnt.outpkts++;
166 s->cnt.outbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100167 u64_stats_update_end(&s->syncp);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300168 rcu_read_unlock();
Hans Schillstromb17fc992011-01-03 14:44:56 +0100169
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200170 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100171 u64_stats_update_begin(&s->syncp);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200172 s->cnt.outpkts++;
173 s->cnt.outbytes += skb->len;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100174 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176}
177
178
179static inline void
180ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
181{
Hans Schillstromb17fc992011-01-03 14:44:56 +0100182 struct netns_ipvs *ipvs = net_ipvs(svc->net);
183 struct ip_vs_cpu_stats *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Hans Schillstromb17fc992011-01-03 14:44:56 +0100185 s = this_cpu_ptr(cp->dest->stats.cpustats);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200186 u64_stats_update_begin(&s->syncp);
187 s->cnt.conns++;
188 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Hans Schillstromb17fc992011-01-03 14:44:56 +0100190 s = this_cpu_ptr(svc->stats.cpustats);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200191 u64_stats_update_begin(&s->syncp);
192 s->cnt.conns++;
193 u64_stats_update_end(&s->syncp);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100194
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200195 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Julian Anastasovcd67cd52015-02-06 09:44:44 +0200196 u64_stats_update_begin(&s->syncp);
197 s->cnt.conns++;
198 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201
Simon Horman4a516f12011-09-16 14:11:49 +0900202static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203ip_vs_set_state(struct ip_vs_conn *cp, int direction,
204 const struct sk_buff *skb,
Hans Schillstrom93304192011-01-03 14:44:51 +0100205 struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Simon Horman4a516f12011-09-16 14:11:49 +0900207 if (likely(pd->pp->state_transition))
208 pd->pp->state_transition(cp, direction, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Hans Schillstroma5959d52010-11-19 14:25:10 +0100211static inline int
Simon Horman85999282010-08-22 21:37:53 +0900212ip_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 Schillstrom6e67e582011-01-03 14:44:57 +0100218 ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr,
219 vport, p);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200220 p->pe = rcu_dereference(svc->pe);
Simon Horman85999282010-08-22 21:37:53 +0900221 if (p->pe && p->pe->fill_param)
Hans Schillstroma5959d52010-11-19 14:25:10 +0100222 return p->pe->fill_param(p, skb);
223
224 return 0;
Simon Horman85999282010-08-22 21:37:53 +0900225}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/*
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 */
234static struct ip_vs_conn *
235ip_vs_sched_persist(struct ip_vs_service *svc,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200236 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
237 int *ignored, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 struct ip_vs_conn *cp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct ip_vs_dest *dest;
241 struct ip_vs_conn *ct;
Simon Horman5b57a982010-08-22 21:37:51 +0900242 __be16 dport = 0; /* destination port to forward */
Julian Anastasov35757922010-09-17 14:18:16 +0200243 unsigned int flags;
Simon Hormanf11017e2010-08-22 21:37:52 +0900244 struct ip_vs_conn_param param;
Simon Hormane0aac522012-01-27 10:45:27 +0900245 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
Julius Volz28364a52008-09-02 15:55:43 +0200246 union nf_inet_addr snet; /* source network of the client,
247 after masking */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* Mask saddr with the netmask to adjust template granularity */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200250#ifdef CONFIG_IP_VS_IPV6
251 if (svc->af == AF_INET6)
Julian Anastasov0a925862013-04-17 23:50:49 +0300252 ipv6_addr_prefix(&snet.in6, &iph->saddr.in6,
253 (__force __u32) svc->netmask);
Julius Volzcd17f9e2008-09-02 15:55:46 +0200254 else
255#endif
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200256 snet.ip = iph->saddr.ip & svc->netmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Julius Volzcd17f9e2008-09-02 15:55:46 +0200258 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
259 "mnet %s\n",
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200260 IP_VS_DBG_ADDR(svc->af, &iph->saddr), ntohs(src_port),
261 IP_VS_DBG_ADDR(svc->af, &iph->daddr), ntohs(dst_port),
Julius Volzcd17f9e2008-09-02 15:55:46 +0200262 IP_VS_DBG_ADDR(svc->af, &snet));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
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 Horman5b57a982010-08-22 21:37:51 +0900277 {
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200278 int protocol = iph->protocol;
279 const union nf_inet_addr *vaddr = &iph->daddr;
Simon Hormanf11017e2010-08-22 21:37:52 +0900280 __be16 vport = 0;
281
Hans Schillstromce144f22010-11-19 14:25:08 +0100282 if (dst_port == svc->port) {
Simon Horman5b57a982010-08-22 21:37:51 +0900283 /* non-FTP template:
284 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
285 * FTP template:
286 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
288 if (svc->port != FTPPORT)
Hans Schillstromce144f22010-11-19 14:25:08 +0100289 vport = dst_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900291 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700297 */
Julius Volz28364a52008-09-02 15:55:43 +0200298 if (svc->fwmark) {
Simon Horman5b57a982010-08-22 21:37:51 +0900299 protocol = IPPROTO_IP;
300 vaddr = &fwmark;
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Hans Schillstroma5959d52010-11-19 14:25:10 +0100303 /* 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, &param) < 0) {
306 *ignored = -1;
307 return NULL;
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
Simon Horman5b57a982010-08-22 21:37:51 +0900311 /* Check if a template already exists */
Simon Hormanf11017e2010-08-22 21:37:52 +0900312 ct = ip_vs_ct_in_get(&param);
Simon Horman5b57a982010-08-22 21:37:51 +0900313 if (!ct || !ip_vs_check_template(ct)) {
Julian Anastasovceec4c32013-03-22 11:46:53 +0200314 struct ip_vs_scheduler *sched;
315
Hans Schillstroma5959d52010-11-19 14:25:10 +0100316 /*
317 * No template found or the dest of the connection
Simon Horman5b57a982010-08-22 21:37:51 +0900318 * template is not available.
Hans Schillstroma5959d52010-11-19 14:25:10 +0100319 * return *ignored=0 i.e. ICMP and NF_DROP
Simon Horman5b57a982010-08-22 21:37:51 +0900320 */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200321 sched = rcu_dereference(svc->scheduler);
Julian Anastasov05f00502015-06-29 21:51:40 +0300322 if (sched) {
323 /* read svc->sched_data after svc->scheduler */
324 smp_rmb();
325 dest = sched->schedule(svc, skb, iph);
326 } else {
327 dest = NULL;
328 }
Simon Horman5b57a982010-08-22 21:37:51 +0900329 if (!dest) {
330 IP_VS_DBG(1, "p-schedule: no dest found.\n");
Simon Horman85999282010-08-22 21:37:53 +0900331 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100332 *ignored = 0;
Simon Horman5b57a982010-08-22 21:37:51 +0900333 return NULL;
334 }
335
Hans Schillstromce144f22010-11-19 14:25:08 +0100336 if (dst_port == svc->port && svc->port != FTPPORT)
Simon Horman5b57a982010-08-22 21:37:51 +0900337 dport = dest->port;
338
Simon Horman85999282010-08-22 21:37:53 +0900339 /* Create a template
340 * This adds param.pe_data to the template,
341 * and thus param.pe_data will be destroyed
342 * when the template expires */
Alex Gartrellba385282014-09-09 16:40:23 -0700343 ct = ip_vs_conn_new(&param, dest->af, &dest->addr, dport,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100344 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
Simon Horman85999282010-08-22 21:37:53 +0900345 if (ct == NULL) {
346 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100347 *ignored = -1;
Simon Horman5b57a982010-08-22 21:37:51 +0900348 return NULL;
Simon Horman85999282010-08-22 21:37:53 +0900349 }
Simon Horman5b57a982010-08-22 21:37:51 +0900350
351 ct->timeout = svc->timeout;
Simon Horman85999282010-08-22 21:37:53 +0900352 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900353 /* set destination with the found template */
354 dest = ct->dest;
Simon Horman85999282010-08-22 21:37:53 +0900355 kfree(param.pe_data);
356 }
Simon Horman5b57a982010-08-22 21:37:51 +0900357
Hans Schillstromce144f22010-11-19 14:25:08 +0100358 dport = dst_port;
Simon Horman5b57a982010-08-22 21:37:51 +0900359 if (dport == svc->port && dest->port)
360 dport = dest->port;
361
Nick Chalk26ec0372010-06-22 08:07:01 +0200362 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200363 && iph->protocol == IPPROTO_UDP) ?
Nick Chalk26ec0372010-06-22 08:07:01 +0200364 IP_VS_CONN_F_ONE_PACKET : 0;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /*
367 * Create a new connection according to the template
368 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200369 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol, &iph->saddr,
370 src_port, &iph->daddr, dst_port, &param);
Hans Schillstromce144f22010-11-19 14:25:08 +0100371
Alex Gartrellba385282014-09-09 16:40:23 -0700372 cp = ip_vs_conn_new(&param, dest->af, &dest->addr, dport, flags, dest,
373 skb->mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (cp == NULL) {
375 ip_vs_conn_put(ct);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100376 *ignored = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return NULL;
378 }
379
380 /*
381 * Add its control
382 */
383 ip_vs_control_add(cp, ct);
384 ip_vs_conn_put(ct);
385
386 ip_vs_conn_stats(cp, svc);
387 return cp;
388}
389
390
391/*
392 * IPVS main scheduling function
393 * It selects a server according to the virtual service, and
394 * creates a connection entry.
395 * Protocols supported: TCP, UDP
Hans Schillstroma5959d52010-11-19 14:25:10 +0100396 *
397 * Usage of *ignored
398 *
399 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
400 * svc/scheduler decides that this packet should be accepted with
401 * NF_ACCEPT because it must not be scheduled.
402 *
403 * 0 : scheduler can not find destination, so try bypass or
404 * return ICMP and then NF_DROP (ip_vs_leave).
405 *
406 * -1 : scheduler tried to schedule but fatal error occurred, eg.
407 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
408 * failure such as missing Call-ID, ENOMEM on skb_linearize
409 * or pe_data. In this case we should return NF_DROP without
410 * any attempts to send ICMP with ip_vs_leave.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 */
412struct ip_vs_conn *
Julian Anastasov190ecd22010-10-17 16:24:37 +0300413ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200414 struct ip_vs_proto_data *pd, int *ignored,
415 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Hans Schillstrom93304192011-01-03 14:44:51 +0100417 struct ip_vs_protocol *pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct ip_vs_conn *cp = NULL;
Julian Anastasovceec4c32013-03-22 11:46:53 +0200419 struct ip_vs_scheduler *sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct ip_vs_dest *dest;
Julian Anastasov35757922010-09-17 14:18:16 +0200421 __be16 _ports[2], *pptr;
422 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Julian Anastasov190ecd22010-10-17 16:24:37 +0300424 *ignored = 1;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200425 /*
426 * IPv6 frags, only the first hit here.
427 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200428 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (pptr == NULL)
430 return NULL;
431
432 /*
Julian Anastasov190ecd22010-10-17 16:24:37 +0300433 * FTPDATA needs this check when using local real server.
434 * Never schedule Active FTPDATA connections from real server.
435 * For LVS-NAT they must be already created. For other methods
436 * with persistence the connection is created on SYN+ACK.
437 */
438 if (pptr[0] == FTPDATA) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300439 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
440 "Not scheduling FTPDATA");
Julian Anastasov190ecd22010-10-17 16:24:37 +0300441 return NULL;
442 }
443
444 /*
Hans Schillstroma5959d52010-11-19 14:25:10 +0100445 * Do not schedule replies from local real server.
Julian Anastasov190ecd22010-10-17 16:24:37 +0300446 */
447 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200448 (cp = pp->conn_in_get(svc->af, skb, iph, 1))) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300449 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
Julian Anastasov190ecd22010-10-17 16:24:37 +0300450 "Not scheduling reply for existing connection");
451 __ip_vs_conn_put(cp);
452 return NULL;
453 }
454
455 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * Persistent service
457 */
Hans Schillstroma5959d52010-11-19 14:25:10 +0100458 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200459 return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored,
460 iph);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100461
462 *ignored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /*
465 * Non-persistent service
466 */
467 if (!svc->fwmark && pptr[1] != svc->port) {
468 if (!svc->port)
Hannes Eder1e3e2382009-08-02 11:05:41 +0000469 pr_err("Schedule: port zero only supported "
470 "in persistent services, "
471 "check your ipvs configuration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return NULL;
473 }
474
Julian Anastasovceec4c32013-03-22 11:46:53 +0200475 sched = rcu_dereference(svc->scheduler);
Julian Anastasov05f00502015-06-29 21:51:40 +0300476 if (sched) {
477 /* read svc->sched_data after svc->scheduler */
478 smp_rmb();
479 dest = sched->schedule(svc, skb, iph);
480 } else {
481 dest = NULL;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (dest == NULL) {
484 IP_VS_DBG(1, "Schedule: no dest found.\n");
485 return NULL;
486 }
487
Nick Chalk26ec0372010-06-22 08:07:01 +0200488 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200489 && iph->protocol == IPPROTO_UDP) ?
Nick Chalk26ec0372010-06-22 08:07:01 +0200490 IP_VS_CONN_F_ONE_PACKET : 0;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /*
493 * Create a connection entry.
494 */
Simon Hormanf11017e2010-08-22 21:37:52 +0900495 {
496 struct ip_vs_conn_param p;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100497
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200498 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
499 &iph->saddr, pptr[0], &iph->daddr,
500 pptr[1], &p);
Alex Gartrellba385282014-09-09 16:40:23 -0700501 cp = ip_vs_conn_new(&p, dest->af, &dest->addr,
Simon Hormanf11017e2010-08-22 21:37:52 +0900502 dest->port ? dest->port : pptr[1],
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100503 flags, dest, skb->mark);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100504 if (!cp) {
505 *ignored = -1;
Simon Hormanf11017e2010-08-22 21:37:52 +0900506 return NULL;
Hans Schillstroma5959d52010-11-19 14:25:10 +0100507 }
Simon Hormanf11017e2010-08-22 21:37:52 +0900508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Julius Volzcd17f9e2008-09-02 15:55:46 +0200510 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
511 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
512 ip_vs_fwd_tag(cp),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700513 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
514 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
515 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
Julius Volzcd17f9e2008-09-02 15:55:46 +0200516 cp->flags, atomic_read(&cp->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 ip_vs_conn_stats(cp, svc);
519 return cp;
520}
521
522
523/*
524 * Pass or drop the packet.
525 * Called by ip_vs_in, when the virtual service is available but
526 * no destination is available for a new connection.
527 */
528int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200529 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Al Viro014d7302006-09-28 14:29:52 -0700531 __be16 _ports[2], *pptr;
Simon Hormana7a86b82011-02-04 18:33:02 +0900532#ifdef CONFIG_SYSCTL
533 struct net *net;
534 struct netns_ipvs *ipvs;
Julius Volz2a3b7912008-09-02 15:55:47 +0200535 int unicast;
Simon Hormana7a86b82011-02-04 18:33:02 +0900536#endif
Hans Schillstrom93304192011-01-03 14:44:51 +0100537
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200538 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (pptr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return NF_DROP;
541 }
Simon Hormana7a86b82011-02-04 18:33:02 +0900542
543#ifdef CONFIG_SYSCTL
Hans Schillstrom4a984802011-01-03 14:45:02 +0100544 net = skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Julius Volz2a3b7912008-09-02 15:55:47 +0200546#ifdef CONFIG_IP_VS_IPV6
547 if (svc->af == AF_INET6)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200548 unicast = ipv6_addr_type(&iph->daddr.in6) & IPV6_ADDR_UNICAST;
Julius Volz2a3b7912008-09-02 15:55:47 +0200549 else
550#endif
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200551 unicast = (inet_addr_type(net, iph->daddr.ip) == RTN_UNICAST);
Julius Volz2a3b7912008-09-02 15:55:47 +0200552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* if it is fwmark-based service, the cache_bypass sysctl is up
Julius Volz2a3b7912008-09-02 15:55:47 +0200554 and the destination is a non-local unicast, then create
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 a cache_bypass connection entry */
Hans Schillstrom4a984802011-01-03 14:45:02 +0100556 ipvs = net_ipvs(net);
Hans Schillstroma0840e22011-01-03 14:44:58 +0100557 if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) {
Krzysztof Wilczynskiad542ced2011-10-18 20:59:49 +0100558 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct ip_vs_conn *cp;
Julian Anastasov35757922010-09-17 14:18:16 +0200560 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200561 iph->protocol == IPPROTO_UDP) ?
Julian Anastasov35757922010-09-17 14:18:16 +0200562 IP_VS_CONN_F_ONE_PACKET : 0;
Simon Hormandff630d2008-09-17 10:10:42 +1000563 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /* create a new connection entry */
Hannes Eder1e3e2382009-08-02 11:05:41 +0000566 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
Simon Hormanf11017e2010-08-22 21:37:52 +0900567 {
568 struct ip_vs_conn_param p;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200569 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
570 &iph->saddr, pptr[0],
571 &iph->daddr, pptr[1], &p);
Alex Gartrellba385282014-09-09 16:40:23 -0700572 cp = ip_vs_conn_new(&p, svc->af, &daddr, 0,
Simon Hormanf11017e2010-08-22 21:37:52 +0900573 IP_VS_CONN_F_BYPASS | flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100574 NULL, skb->mark);
Simon Hormanf11017e2010-08-22 21:37:52 +0900575 if (!cp)
576 return NF_DROP;
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /* statistics */
580 ip_vs_in_stats(cp, skb);
581
582 /* set state */
Simon Horman4a516f12011-09-16 14:11:49 +0900583 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* transmit the first SYN packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200586 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* do not touch skb anymore */
588
589 atomic_inc(&cp->in_pkts);
590 ip_vs_conn_put(cp);
591 return ret;
592 }
Simon Hormana7a86b82011-02-04 18:33:02 +0900593#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /*
596 * When the virtual ftp service is presented, packets destined
597 * for other services on the VIP may get here (except services
598 * listed in the ipvs table), pass the packets, because it is
599 * not ipvs job to decide to drop the packets.
600 */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200601 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /*
605 * Notify the client that the destination is unreachable, and
606 * release the socket buffer.
607 * Since it is in IP layer, the TCP socket is not actually
608 * created, the TCP RST packet cannot be sent, instead that
609 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
610 */
Julius Volz2a3b7912008-09-02 15:55:47 +0200611#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +0300612 if (svc->af == AF_INET6) {
613 if (!skb->dev) {
Simon Horman9fd0fa72013-04-19 10:25:42 +0900614 struct net *net_ = dev_net(skb_dst(skb)->dev);
Julian Anastasovcb591552010-10-17 16:40:51 +0300615
Simon Horman9fd0fa72013-04-19 10:25:42 +0900616 skb->dev = net_->loopback_dev;
Julian Anastasovcb591552010-10-17 16:40:51 +0300617 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000618 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Julian Anastasovcb591552010-10-17 16:40:51 +0300619 } else
Julius Volz2a3b7912008-09-02 15:55:47 +0200620#endif
621 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return NF_DROP;
624}
625
Simon Horman84b3cee2011-02-04 18:33:01 +0900626#ifdef CONFIG_SYSCTL
627
628static int sysctl_snat_reroute(struct sk_buff *skb)
629{
630 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
631 return ipvs->sysctl_snat_reroute;
632}
633
Simon Horman0cfa5582011-02-04 18:33:01 +0900634static int sysctl_nat_icmp_send(struct net *net)
635{
636 struct netns_ipvs *ipvs = net_ipvs(net);
637 return ipvs->sysctl_nat_icmp_send;
638}
639
Simon Horman71a8ab62011-02-04 18:33:01 +0900640static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
641{
642 return ipvs->sysctl_expire_nodest_conn;
643}
644
Simon Horman84b3cee2011-02-04 18:33:01 +0900645#else
646
647static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; }
Simon Horman0cfa5582011-02-04 18:33:01 +0900648static int sysctl_nat_icmp_send(struct net *net) { return 0; }
Simon Horman71a8ab62011-02-04 18:33:01 +0900649static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
Simon Horman84b3cee2011-02-04 18:33:01 +0900650
651#endif
652
Al Virob1550f22006-11-14 21:37:50 -0800653__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Al Virod3bc23e2006-11-14 21:24:49 -0800655 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300658static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
659{
660 if (NF_INET_LOCAL_IN == hooknum)
661 return IP_DEFRAG_VS_IN;
662 if (NF_INET_FORWARD == hooknum)
663 return IP_DEFRAG_VS_FWD;
664 return IP_DEFRAG_VS_OUT;
665}
666
Herbert Xu776c7292007-10-14 00:38:32 -0700667static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Julian Anastasovac692692013-03-22 11:46:54 +0200669 int err;
Herbert Xu776c7292007-10-14 00:38:32 -0700670
Julian Anastasovac692692013-03-22 11:46:54 +0200671 local_bh_disable();
672 err = ip_defrag(skb, user);
673 local_bh_enable();
Herbert Xu776c7292007-10-14 00:38:32 -0700674 if (!err)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700675 ip_send_check(ip_hdr(skb));
Herbert Xu776c7292007-10-14 00:38:32 -0700676
677 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Julian Anastasov579eb622014-12-18 22:41:23 +0200680static int ip_vs_route_me_harder(int af, struct sk_buff *skb,
681 unsigned int hooknum)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900682{
Julian Anastasov579eb622014-12-18 22:41:23 +0200683 if (!sysctl_snat_reroute(skb))
684 return 0;
685 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
686 if (NF_INET_LOCAL_IN == hooknum)
687 return 0;
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900688#ifdef CONFIG_IP_VS_IPV6
689 if (af == AF_INET6) {
Julian Anastasov579eb622014-12-18 22:41:23 +0200690 struct dst_entry *dst = skb_dst(skb);
691
692 if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
693 ip6_route_me_harder(skb) != 0)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900694 return 1;
695 } else
696#endif
Julian Anastasov579eb622014-12-18 22:41:23 +0200697 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900698 ip_route_me_harder(skb, RTN_LOCAL) != 0)
699 return 1;
700
701 return 0;
702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/*
705 * Packet has been made sufficiently writable in caller
706 * - inout: 1=in->out, 0=out->in
707 */
708void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
709 struct ip_vs_conn *cp, int inout)
710{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700711 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 unsigned int icmp_offset = iph->ihl*4;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700713 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
714 icmp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
716
717 if (inout) {
Julius Volze7ade462008-09-02 15:55:33 +0200718 iph->saddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200720 ciph->daddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 ip_send_check(ciph);
722 } else {
Julius Volze7ade462008-09-02 15:55:33 +0200723 iph->daddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200725 ciph->saddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 ip_send_check(ciph);
727 }
728
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100729 /* the TCP/UDP/SCTP port */
730 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
731 IPPROTO_SCTP == ciph->protocol) {
Al Viro014d7302006-09-28 14:29:52 -0700732 __be16 *ports = (void *)ciph + ciph->ihl*4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 if (inout)
735 ports[1] = cp->vport;
736 else
737 ports[0] = cp->dport;
738 }
739
740 /* And finally the ICMP checksum */
741 icmph->checksum = 0;
742 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
743 skb->ip_summed = CHECKSUM_UNNECESSARY;
744
745 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300746 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 "Forwarding altered outgoing ICMP");
748 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300749 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 "Forwarding altered incoming ICMP");
751}
752
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200753#ifdef CONFIG_IP_VS_IPV6
754void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
755 struct ip_vs_conn *cp, int inout)
756{
757 struct ipv6hdr *iph = ipv6_hdr(skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200758 unsigned int icmp_offset = 0;
759 unsigned int offs = 0; /* header offset*/
760 int protocol;
761 struct icmp6hdr *icmph;
762 struct ipv6hdr *ciph;
763 unsigned short fragoffs;
764
765 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
766 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
767 offs = icmp_offset + sizeof(struct icmp6hdr);
768 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
769
770 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200771
772 if (inout) {
773 iph->saddr = cp->vaddr.in6;
774 ciph->daddr = cp->vaddr.in6;
775 } else {
776 iph->daddr = cp->daddr.in6;
777 ciph->saddr = cp->daddr.in6;
778 }
779
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100780 /* the TCP/UDP/SCTP port */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200781 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
782 IPPROTO_SCTP == protocol)) {
783 __be16 *ports = (void *)(skb_network_header(skb) + offs);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200784
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200785 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
786 ntohs(inout ? ports[1] : ports[0]),
787 ntohs(inout ? cp->vport : cp->dport));
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200788 if (inout)
789 ports[1] = cp->vport;
790 else
791 ports[0] = cp->dport;
792 }
793
794 /* And finally the ICMP checksum */
Simon Horman8870f842010-08-26 13:21:26 -0700795 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
796 skb->len - icmp_offset,
797 IPPROTO_ICMPV6, 0);
798 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
799 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
800 skb->ip_summed = CHECKSUM_PARTIAL;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200801
802 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300803 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
804 (void *)ciph - (void *)iph,
805 "Forwarding altered outgoing ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200806 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300807 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
808 (void *)ciph - (void *)iph,
809 "Forwarding altered incoming ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200810}
811#endif
812
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000813/* Handle relevant response ICMP messages - forward to the right
Julian Anastasov6cb90db2011-02-09 02:26:38 +0200814 * destination host.
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000815 */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000816static int handle_response_icmp(int af, struct sk_buff *skb,
817 union nf_inet_addr *snet,
818 __u8 protocol, struct ip_vs_conn *cp,
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000819 struct ip_vs_protocol *pp,
Julian Anastasov579eb622014-12-18 22:41:23 +0200820 unsigned int offset, unsigned int ihl,
821 unsigned int hooknum)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000822{
823 unsigned int verdict = NF_DROP;
824
825 if (IP_VS_FWD_METHOD(cp) != 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000826 pr_err("shouldn't reach here, because the box is on the "
827 "half connection in the tun/dr module.\n");
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000828 }
829
830 /* Ensure the checksum is correct */
831 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
832 /* Failed checksum! */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000833 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
834 IP_VS_DBG_ADDR(af, snet));
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000835 goto out;
836 }
837
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100838 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
839 IPPROTO_SCTP == protocol)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000840 offset += 2 * sizeof(__u16);
841 if (!skb_make_writable(skb, offset))
842 goto out;
843
Simon Hormanf2428ed2008-09-05 11:17:14 +1000844#ifdef CONFIG_IP_VS_IPV6
845 if (af == AF_INET6)
846 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
847 else
848#endif
849 ip_vs_nat_icmp(skb, pp, cp, 1);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000850
Julian Anastasov579eb622014-12-18 22:41:23 +0200851 if (ip_vs_route_me_harder(af, skb, hooknum))
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900852 goto out;
Julian Anastasovf5a41842010-10-17 16:35:46 +0300853
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000854 /* do the statistics and put it back */
855 ip_vs_out_stats(cp, skb);
856
Julian Anastasovcf356d62010-10-17 16:21:07 +0300857 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200858 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +0300859 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200860 else
861 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000862 verdict = NF_ACCEPT;
863
864out:
865 __ip_vs_conn_put(cp);
866
867 return verdict;
868}
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870/*
871 * Handle ICMP messages in the inside-to-outside direction (outgoing).
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000872 * Find any that might be relevant, check against existing connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * Currently handles error types - unreachable, quench, ttl exceeded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 */
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300875static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
876 unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct iphdr *iph;
879 struct icmphdr _icmph, *ic;
880 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +0200881 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct ip_vs_conn *cp;
883 struct ip_vs_protocol *pp;
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000884 unsigned int offset, ihl;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000885 union nf_inet_addr snet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 *related = 1;
888
889 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -0700890 if (ip_is_fragment(ip_hdr(skb))) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300891 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700895 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 offset = ihl = iph->ihl * 4;
897 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
898 if (ic == NULL)
899 return NF_DROP;
900
Harvey Harrison14d5e832008-10-31 00:54:29 -0700901 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -0700903 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 /*
906 * Work through seeing if this is for us.
907 * These checks are supposed to be in an order that means easy
908 * things are checked first to speed up processing.... however
909 * this means that some packets will manage to get a long way
910 * down this stack and then be rejected, but that's life.
911 */
912 if ((ic->type != ICMP_DEST_UNREACH) &&
913 (ic->type != ICMP_SOURCE_QUENCH) &&
914 (ic->type != ICMP_TIME_EXCEEDED)) {
915 *related = 0;
916 return NF_ACCEPT;
917 }
918
919 /* Now find the contained IP header */
920 offset += sizeof(_icmph);
921 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
922 if (cih == NULL)
923 return NF_ACCEPT; /* The packet looks wrong, ignore */
924
925 pp = ip_vs_proto_get(cih->protocol);
926 if (!pp)
927 return NF_ACCEPT;
928
929 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900930 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 pp->dont_defrag))
932 return NF_ACCEPT;
933
Julian Anastasov0d796412010-10-17 16:46:17 +0300934 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
935 "Checking outgoing ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200937 ip_vs_fill_ip4hdr(cih, &ciph);
938 ciph.len += offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /* The embedded headers contain source and dest in reverse order */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200940 cp = pp->conn_out_get(AF_INET, skb, &ciph, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (!cp)
942 return NF_ACCEPT;
943
Simon Hormanf2428ed2008-09-05 11:17:14 +1000944 snet.ip = iph->saddr;
945 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
Julian Anastasov579eb622014-12-18 22:41:23 +0200946 pp, ciph.len, ihl, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Julius Volz2a3b7912008-09-02 15:55:47 +0200949#ifdef CONFIG_IP_VS_IPV6
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300950static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200951 unsigned int hooknum, struct ip_vs_iphdr *ipvsh)
Julius Volz2a3b7912008-09-02 15:55:47 +0200952{
Julius Volz2a3b7912008-09-02 15:55:47 +0200953 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200954 struct ipv6hdr _ip6h, *ip6h; /* The ip header contained within ICMP */
955 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +0200956 struct ip_vs_conn *cp;
957 struct ip_vs_protocol *pp;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000958 union nf_inet_addr snet;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200959 unsigned int writable;
960
Julius Volz2a3b7912008-09-02 15:55:47 +0200961 *related = 1;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200962 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
Julius Volz2a3b7912008-09-02 15:55:47 +0200963 if (ic == NULL)
964 return NF_DROP;
965
Julius Volz2a3b7912008-09-02 15:55:47 +0200966 /*
967 * Work through seeing if this is for us.
968 * These checks are supposed to be in an order that means easy
969 * things are checked first to speed up processing.... however
970 * this means that some packets will manage to get a long way
971 * down this stack and then be rejected, but that's life.
972 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +0200973 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +0200974 *related = 0;
975 return NF_ACCEPT;
976 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200977 /* Fragment header that is before ICMP header tells us that:
978 * it's not an error message since they can't be fragmented.
979 */
David S. Millere7165032012-11-30 12:01:30 -0500980 if (ipvsh->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200981 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +0200982
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200983 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
984 ic->icmp6_type, ntohs(icmpv6_id(ic)),
985 &ipvsh->saddr, &ipvsh->daddr);
Julius Volz2a3b7912008-09-02 15:55:47 +0200986
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200987 /* Now find the contained IP header */
988 ciph.len = ipvsh->len + sizeof(_icmph);
989 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
990 if (ip6h == NULL)
991 return NF_ACCEPT; /* The packet looks wrong, ignore */
992 ciph.saddr.in6 = ip6h->saddr; /* conn_out_get() handles reverse order */
993 ciph.daddr.in6 = ip6h->daddr;
994 /* skip possible IPv6 exthdrs of contained IPv6 packet */
995 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
996 if (ciph.protocol < 0)
997 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
998
999 pp = ip_vs_proto_get(ciph.protocol);
Julius Volz2a3b7912008-09-02 15:55:47 +02001000 if (!pp)
1001 return NF_ACCEPT;
1002
Julius Volz2a3b7912008-09-02 15:55:47 +02001003 /* The embedded headers contain source and dest in reverse order */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001004 cp = pp->conn_out_get(AF_INET6, skb, &ciph, 1);
Julius Volz2a3b7912008-09-02 15:55:47 +02001005 if (!cp)
1006 return NF_ACCEPT;
1007
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001008 snet.in6 = ciph.saddr.in6;
1009 writable = ciph.len;
1010 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
Julian Anastasov579eb622014-12-18 22:41:23 +02001011 pp, writable, sizeof(struct ipv6hdr),
1012 hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +02001013}
1014#endif
1015
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +01001016/*
1017 * Check if sctp chunc is ABORT chunk
1018 */
1019static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
1020{
1021 sctp_chunkhdr_t *sch, schunk;
1022 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
1023 sizeof(schunk), &schunk);
1024 if (sch == NULL)
1025 return 0;
1026 if (sch->type == SCTP_CID_ABORT)
1027 return 1;
1028 return 0;
1029}
1030
Julius Volz2a3b7912008-09-02 15:55:47 +02001031static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 struct tcphdr _tcph, *th;
1034
Julius Volz2a3b7912008-09-02 15:55:47 +02001035 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (th == NULL)
1037 return 0;
1038 return th->rst;
1039}
1040
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001041static inline bool is_new_conn(const struct sk_buff *skb,
1042 struct ip_vs_iphdr *iph)
1043{
1044 switch (iph->protocol) {
1045 case IPPROTO_TCP: {
1046 struct tcphdr _tcph, *th;
1047
1048 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1049 if (th == NULL)
1050 return false;
1051 return th->syn;
1052 }
1053 case IPPROTO_SCTP: {
1054 sctp_chunkhdr_t *sch, schunk;
1055
1056 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
1057 sizeof(schunk), &schunk);
1058 if (sch == NULL)
1059 return false;
1060 return sch->type == SCTP_CID_INIT;
1061 }
1062 default:
1063 return false;
1064 }
1065}
1066
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001067static inline bool is_new_conn_expected(const struct ip_vs_conn *cp,
1068 int conn_reuse_mode)
1069{
1070 /* Controlled (FTP DATA or persistence)? */
1071 if (cp->control)
1072 return false;
1073
1074 switch (cp->protocol) {
1075 case IPPROTO_TCP:
1076 return (cp->state == IP_VS_TCP_S_TIME_WAIT) ||
1077 ((conn_reuse_mode & 2) &&
1078 (cp->state == IP_VS_TCP_S_FIN_WAIT) &&
1079 (cp->flags & IP_VS_CONN_F_NOOUTPUT));
1080 case IPPROTO_SCTP:
1081 return cp->state == IP_VS_SCTP_S_CLOSED;
1082 default:
1083 return false;
1084 }
1085}
1086
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001087/* Handle response packets: rewrite addresses and send away...
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001088 */
1089static unsigned int
Hans Schillstrom93304192011-01-03 14:44:51 +01001090handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
Julian Anastasov579eb622014-12-18 22:41:23 +02001091 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph,
1092 unsigned int hooknum)
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001093{
Hans Schillstrom93304192011-01-03 14:44:51 +01001094 struct ip_vs_protocol *pp = pd->pp;
1095
Julian Anastasov0d796412010-10-17 16:46:17 +03001096 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001097
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001098 if (!skb_make_writable(skb, iph->len))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001099 goto drop;
1100
1101 /* mangle the packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001102 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001103 goto drop;
1104
1105#ifdef CONFIG_IP_VS_IPV6
1106 if (af == AF_INET6)
1107 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1108 else
1109#endif
1110 {
1111 ip_hdr(skb)->saddr = cp->vaddr.ip;
1112 ip_send_check(ip_hdr(skb));
1113 }
1114
Julian Anastasov8a803042010-09-21 17:38:57 +02001115 /*
1116 * nf_iterate does not expect change in the skb->dst->dev.
1117 * It looks like it is not fatal to enable this code for hooks
1118 * where our handlers are at the end of the chain list and
1119 * when all next handlers use skb->dst->dev and not outdev.
1120 * It will definitely route properly the inout NAT traffic
1121 * when multiple paths are used.
1122 */
1123
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001124 /* For policy routing, packets originating from this
1125 * machine itself may be routed differently to packets
1126 * passing through. We want this packet to be routed as
1127 * if it came from this machine itself. So re-compute
1128 * the routing information.
1129 */
Julian Anastasov579eb622014-12-18 22:41:23 +02001130 if (ip_vs_route_me_harder(af, skb, hooknum))
Simon Hormanba4fd7e2011-02-04 18:33:01 +09001131 goto drop;
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001132
Julian Anastasov0d796412010-10-17 16:46:17 +03001133 IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001134
1135 ip_vs_out_stats(cp, skb);
Hans Schillstrom93304192011-01-03 14:44:51 +01001136 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
Julian Anastasovcf356d62010-10-17 16:21:07 +03001137 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001138 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +03001139 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001140 else
1141 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001142 ip_vs_conn_put(cp);
1143
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001144 LeaveFunction(11);
1145 return NF_ACCEPT;
1146
1147drop:
1148 ip_vs_conn_put(cp);
1149 kfree_skb(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001150 LeaveFunction(11);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001151 return NF_STOLEN;
1152}
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/*
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001155 * Check if outgoing packet belongs to the established ip_vs_conn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 */
1157static unsigned int
Julian Anastasovfc604762010-10-17 16:38:15 +03001158ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Hans Schillstromfc723252011-01-03 14:44:43 +01001160 struct net *net = NULL;
Julius Volz51ef3482008-09-02 15:55:40 +02001161 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001163 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 EnterFunction(11);
1167
Julian Anastasovfc604762010-10-17 16:38:15 +03001168 /* Already marked as IPVS request or reply? */
Harald Welte6869c4d2005-08-09 19:24:19 -07001169 if (skb->ipvs_property)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return NF_ACCEPT;
1171
Julian Anastasovfc604762010-10-17 16:38:15 +03001172 /* Bad... Do not break raw sockets */
1173 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1174 af == AF_INET)) {
1175 struct sock *sk = skb->sk;
1176 struct inet_sock *inet = inet_sk(skb->sk);
1177
1178 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1179 return NF_ACCEPT;
1180 }
1181
1182 if (unlikely(!skb_dst(skb)))
1183 return NF_ACCEPT;
1184
Hans Schillstromfc723252011-01-03 14:44:43 +01001185 net = skb_net(skb);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001186 if (!net_ipvs(net)->enable)
1187 return NF_ACCEPT;
1188
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001189 ip_vs_fill_iph_skb(af, skb, &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001190#ifdef CONFIG_IP_VS_IPV6
1191 if (af == AF_INET6) {
1192 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001193 int related;
1194 int verdict = ip_vs_out_icmp_v6(skb, &related,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001195 hooknum, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Julian Anastasovf5a41842010-10-17 16:35:46 +03001197 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001198 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001199 }
1200 } else
1201#endif
1202 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001203 int related;
1204 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +02001205
Julian Anastasovf5a41842010-10-17 16:35:46 +03001206 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001207 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Hans Schillstrom93304192011-01-03 14:44:51 +01001210 pd = ip_vs_proto_data_get(net, iph.protocol);
1211 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001213 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /* reassemble IP fragments */
Julius Volz2a3b7912008-09-02 15:55:47 +02001216#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001217 if (af == AF_INET)
Julius Volz2a3b7912008-09-02 15:55:47 +02001218#endif
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001219 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001220 if (ip_vs_gather_frags(skb,
1221 ip_vs_defrag_user(hooknum)))
Julius Volz2a3b7912008-09-02 15:55:47 +02001222 return NF_STOLEN;
1223
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001224 ip_vs_fill_ip4hdr(skb_network_header(skb), &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 /*
1228 * Check if the packet belongs to an existing entry
1229 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001230 cp = pp->conn_out_get(af, skb, &iph, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Julian Anastasovcb591552010-10-17 16:40:51 +03001232 if (likely(cp))
Julian Anastasov579eb622014-12-18 22:41:23 +02001233 return handle_response(af, skb, pd, cp, &iph, hooknum);
Simon Horman0cfa5582011-02-04 18:33:01 +09001234 if (sysctl_nat_icmp_send(net) &&
Julian Anastasovcb591552010-10-17 16:40:51 +03001235 (pp->protocol == IPPROTO_TCP ||
1236 pp->protocol == IPPROTO_UDP ||
1237 pp->protocol == IPPROTO_SCTP)) {
1238 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001240 pptr = frag_safe_skb_hp(skb, iph.len,
1241 sizeof(_ports), _ports, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001242 if (pptr == NULL)
1243 return NF_ACCEPT; /* Not for me */
Julian Anastasov276472e2013-03-21 11:58:08 +02001244 if (ip_vs_has_real_service(net, af, iph.protocol, &iph.saddr,
1245 pptr[0])) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001246 /*
1247 * Notify the real server: there is no
1248 * existing entry if it is not RST
1249 * packet or not TCP packet.
1250 */
1251 if ((iph.protocol != IPPROTO_TCP &&
1252 iph.protocol != IPPROTO_SCTP)
1253 || ((iph.protocol == IPPROTO_TCP
1254 && !is_tcp_reset(skb, iph.len))
1255 || (iph.protocol == IPPROTO_SCTP
1256 && !is_sctp_abort(skb,
1257 iph.len)))) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001258#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001259 if (af == AF_INET6) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001260 if (!skb->dev)
1261 skb->dev = net->loopback_dev;
1262 icmpv6_send(skb,
1263 ICMPV6_DEST_UNREACH,
1264 ICMPV6_PORT_UNREACH,
1265 0);
1266 } else
Julius Volz2a3b7912008-09-02 15:55:47 +02001267#endif
Julian Anastasovcb591552010-10-17 16:40:51 +03001268 icmp_send(skb,
1269 ICMP_DEST_UNREACH,
1270 ICMP_PORT_UNREACH, 0);
1271 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Julian Anastasov0d796412010-10-17 16:46:17 +03001275 IP_VS_DBG_PKT(12, af, pp, skb, 0,
Julian Anastasovcb591552010-10-17 16:40:51 +03001276 "ip_vs_out: packet continues traversal as normal");
1277 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Julian Anastasovfc604762010-10-17 16:38:15 +03001280/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001281 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1282 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001283 * Check if packet is reply for established ip_vs_conn.
1284 */
1285static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001286ip_vs_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001287 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001288{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001289 return ip_vs_out(ops->hooknum, skb, AF_INET);
Julian Anastasovfc604762010-10-17 16:38:15 +03001290}
1291
1292/*
1293 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1294 * Check if packet is reply for established ip_vs_conn.
1295 */
1296static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001297ip_vs_local_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001298 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001299{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001300 return ip_vs_out(ops->hooknum, skb, AF_INET);
Julian Anastasovfc604762010-10-17 16:38:15 +03001301}
1302
1303#ifdef CONFIG_IP_VS_IPV6
1304
1305/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001306 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1307 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001308 * Check if packet is reply for established ip_vs_conn.
1309 */
1310static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001311ip_vs_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001312 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001313{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001314 return ip_vs_out(ops->hooknum, skb, AF_INET6);
Julian Anastasovfc604762010-10-17 16:38:15 +03001315}
1316
1317/*
1318 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1319 * Check if packet is reply for established ip_vs_conn.
1320 */
1321static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001322ip_vs_local_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001323 const struct nf_hook_state *state)
Julian Anastasovfc604762010-10-17 16:38:15 +03001324{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001325 return ip_vs_out(ops->hooknum, skb, AF_INET6);
Julian Anastasovfc604762010-10-17 16:38:15 +03001326}
1327
1328#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330/*
1331 * Handle ICMP messages in the outside-to-inside direction (incoming).
1332 * Find any that might be relevant, check against existing connections,
1333 * forward to the right destination host if relevant.
1334 * Currently handles error types - unreachable, quench, ttl exceeded.
1335 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001336static int
Herbert Xu3db05fe2007-10-15 00:53:15 -07001337ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Hans Schillstrom93304192011-01-03 14:44:51 +01001339 struct net *net = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 struct iphdr *iph;
1341 struct icmphdr _icmph, *ic;
1342 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +02001343 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 struct ip_vs_conn *cp;
1345 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001346 struct ip_vs_proto_data *pd;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001347 unsigned int offset, offset2, ihl, verdict;
1348 bool ipip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 *related = 1;
1351
1352 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001353 if (ip_is_fragment(ip_hdr(skb))) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001354 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001358 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 offset = ihl = iph->ihl * 4;
1360 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1361 if (ic == NULL)
1362 return NF_DROP;
1363
Harvey Harrison14d5e832008-10-31 00:54:29 -07001364 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -07001366 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 /*
1369 * Work through seeing if this is for us.
1370 * These checks are supposed to be in an order that means easy
1371 * things are checked first to speed up processing.... however
1372 * this means that some packets will manage to get a long way
1373 * down this stack and then be rejected, but that's life.
1374 */
1375 if ((ic->type != ICMP_DEST_UNREACH) &&
1376 (ic->type != ICMP_SOURCE_QUENCH) &&
1377 (ic->type != ICMP_TIME_EXCEEDED)) {
1378 *related = 0;
1379 return NF_ACCEPT;
1380 }
1381
1382 /* Now find the contained IP header */
1383 offset += sizeof(_icmph);
1384 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1385 if (cih == NULL)
1386 return NF_ACCEPT; /* The packet looks wrong, ignore */
1387
Hans Schillstrom93304192011-01-03 14:44:51 +01001388 net = skb_net(skb);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001389
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001390 /* Special case for errors for IPIP packets */
1391 ipip = false;
1392 if (cih->protocol == IPPROTO_IPIP) {
1393 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1394 return NF_ACCEPT;
1395 /* Error for our IPIP must arrive at LOCAL_IN */
1396 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1397 return NF_ACCEPT;
1398 offset += cih->ihl * 4;
1399 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1400 if (cih == NULL)
1401 return NF_ACCEPT; /* The packet looks wrong, ignore */
1402 ipip = true;
1403 }
1404
Hans Schillstrom93304192011-01-03 14:44:51 +01001405 pd = ip_vs_proto_data_get(net, cih->protocol);
1406 if (!pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001408 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +09001411 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 pp->dont_defrag))
1413 return NF_ACCEPT;
1414
Julian Anastasov0d796412010-10-17 16:46:17 +03001415 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1416 "Checking incoming ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001418 offset2 = offset;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001419 ip_vs_fill_ip4hdr(cih, &ciph);
1420 ciph.len += offset;
1421 offset = ciph.len;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001422 /* The embedded headers contain source and dest in reverse order.
1423 * For IPIP this is error for request, not for reply.
1424 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001425 cp = pp->conn_in_get(AF_INET, skb, &ciph, ipip ? 0 : 1);
Julian Anastasov6cb90db2011-02-09 02:26:38 +02001426 if (!cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return NF_ACCEPT;
1428
1429 verdict = NF_DROP;
1430
1431 /* Ensure the checksum is correct */
Herbert Xu60476372007-04-09 11:59:39 -07001432 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /* Failed checksum! */
Harvey Harrison14d5e832008-10-31 00:54:29 -07001434 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1435 &iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 goto out;
1437 }
1438
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001439 if (ipip) {
1440 __be32 info = ic->un.gateway;
Peter Christensenf44a5f42014-05-24 21:40:12 +02001441 __u8 type = ic->type;
1442 __u8 code = ic->code;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001443
1444 /* Update the MTU */
1445 if (ic->type == ICMP_DEST_UNREACH &&
1446 ic->code == ICMP_FRAG_NEEDED) {
1447 struct ip_vs_dest *dest = cp->dest;
1448 u32 mtu = ntohs(ic->un.frag.mtu);
Peter Christensenf44a5f42014-05-24 21:40:12 +02001449 __be16 frag_off = cih->frag_off;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001450
1451 /* Strip outer IP and ICMP, go to IPIP header */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001452 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1453 goto ignore_ipip;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001454 offset2 -= ihl + sizeof(_icmph);
1455 skb_reset_network_header(skb);
1456 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1457 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001458 ipv4_update_pmtu(skb, dev_net(skb->dev),
1459 mtu, 0, 0, 0, 0);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001460 /* Client uses PMTUD? */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001461 if (!(frag_off & htons(IP_DF)))
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001462 goto ignore_ipip;
1463 /* Prefer the resulting PMTU */
1464 if (dest) {
Julian Anastasov026ace02013-03-21 11:58:06 +02001465 struct ip_vs_dest_dst *dest_dst;
1466
1467 rcu_read_lock();
1468 dest_dst = rcu_dereference(dest->dest_dst);
1469 if (dest_dst)
1470 mtu = dst_mtu(dest_dst->dst_cache);
1471 rcu_read_unlock();
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001472 }
1473 if (mtu > 68 + sizeof(struct iphdr))
1474 mtu -= sizeof(struct iphdr);
1475 info = htonl(mtu);
1476 }
1477 /* Strip outer IP, ICMP and IPIP, go to IP header of
1478 * original request.
1479 */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001480 if (pskb_pull(skb, offset2) == NULL)
1481 goto ignore_ipip;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001482 skb_reset_network_header(skb);
1483 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1484 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
Peter Christensenf44a5f42014-05-24 21:40:12 +02001485 type, code, ntohl(info));
1486 icmp_send(skb, type, code, info);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001487 /* ICMP can be shorter but anyways, account it */
1488 ip_vs_out_stats(cp, skb);
1489
1490ignore_ipip:
1491 consume_skb(skb);
1492 verdict = NF_STOLEN;
1493 goto out;
1494 }
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* do the statistics and put it back */
1497 ip_vs_in_stats(cp, skb);
Julian Anastasov06f3d7f2013-06-18 10:08:06 +03001498 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1499 IPPROTO_SCTP == cih->protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 offset += 2 * sizeof(__u16);
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001501 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Hans Schillstrom552ad652011-06-13 12:19:26 +02001503out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 __ip_vs_conn_put(cp);
1505
1506 return verdict;
1507}
1508
Julius Volz2a3b7912008-09-02 15:55:47 +02001509#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001510static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
1511 unsigned int hooknum, struct ip_vs_iphdr *iph)
Julius Volz2a3b7912008-09-02 15:55:47 +02001512{
Hans Schillstrom93304192011-01-03 14:44:51 +01001513 struct net *net = NULL;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001514 struct ipv6hdr _ip6h, *ip6h;
Julius Volz2a3b7912008-09-02 15:55:47 +02001515 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001516 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +02001517 struct ip_vs_conn *cp;
1518 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001519 struct ip_vs_proto_data *pd;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001520 unsigned int offs_ciph, writable, verdict;
1521
Julius Volz2a3b7912008-09-02 15:55:47 +02001522 *related = 1;
1523
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001524 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001525 if (ic == NULL)
1526 return NF_DROP;
1527
Julius Volz2a3b7912008-09-02 15:55:47 +02001528 /*
1529 * Work through seeing if this is for us.
1530 * These checks are supposed to be in an order that means easy
1531 * things are checked first to speed up processing.... however
1532 * this means that some packets will manage to get a long way
1533 * down this stack and then be rejected, but that's life.
1534 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +02001535 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001536 *related = 0;
1537 return NF_ACCEPT;
1538 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001539 /* Fragment header that is before ICMP header tells us that:
1540 * it's not an error message since they can't be fragmented.
1541 */
David S. Millere7165032012-11-30 12:01:30 -05001542 if (iph->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001543 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +02001544
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001545 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1546 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1547 &iph->saddr, &iph->daddr);
1548
Julius Volz2a3b7912008-09-02 15:55:47 +02001549 /* Now find the contained IP header */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001550 ciph.len = iph->len + sizeof(_icmph);
1551 offs_ciph = ciph.len; /* Save ip header offset */
1552 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
1553 if (ip6h == NULL)
Julius Volz2a3b7912008-09-02 15:55:47 +02001554 return NF_ACCEPT; /* The packet looks wrong, ignore */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001555 ciph.saddr.in6 = ip6h->saddr; /* conn_in_get() handles reverse order */
1556 ciph.daddr.in6 = ip6h->daddr;
1557 /* skip possible IPv6 exthdrs of contained IPv6 packet */
1558 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
1559 if (ciph.protocol < 0)
1560 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
Julius Volz2a3b7912008-09-02 15:55:47 +02001561
Hans Schillstrom93304192011-01-03 14:44:51 +01001562 net = skb_net(skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001563 pd = ip_vs_proto_data_get(net, ciph.protocol);
Hans Schillstrom93304192011-01-03 14:44:51 +01001564 if (!pd)
Julius Volz2a3b7912008-09-02 15:55:47 +02001565 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001566 pp = pd->pp;
Julius Volz2a3b7912008-09-02 15:55:47 +02001567
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001568 /* Cannot handle fragmented embedded protocol */
1569 if (ciph.fragoffs)
Julius Volz2a3b7912008-09-02 15:55:47 +02001570 return NF_ACCEPT;
1571
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001572 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offs_ciph,
Julian Anastasov0d796412010-10-17 16:46:17 +03001573 "Checking incoming ICMPv6 for");
Julius Volz2a3b7912008-09-02 15:55:47 +02001574
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001575 /* The embedded headers contain source and dest in reverse order
1576 * if not from localhost
1577 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001578 cp = pp->conn_in_get(AF_INET6, skb, &ciph,
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001579 (hooknum == NF_INET_LOCAL_OUT) ? 0 : 1);
1580
Julian Anastasov6cb90db2011-02-09 02:26:38 +02001581 if (!cp)
Julius Volz2a3b7912008-09-02 15:55:47 +02001582 return NF_ACCEPT;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001583 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1584 if ((hooknum == NF_INET_LOCAL_OUT) &&
1585 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
1586 __ip_vs_conn_put(cp);
1587 return NF_ACCEPT;
1588 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001589
Julius Volz2a3b7912008-09-02 15:55:47 +02001590 /* do the statistics and put it back */
1591 ip_vs_in_stats(cp, skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001592
1593 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1594 writable = ciph.len;
1595 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1596 IPPROTO_SCTP == ciph.protocol)
1597 writable += 2 * sizeof(__u16); /* Also mangle ports */
1598
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001599 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, writable, hooknum, &ciph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001600
1601 __ip_vs_conn_put(cp);
1602
1603 return verdict;
1604}
1605#endif
1606
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608/*
1609 * Check if it's for virtual services, look it up,
1610 * and send it on its way...
1611 */
1612static unsigned int
Julian Anastasovcb591552010-10-17 16:40:51 +03001613ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Hans Schillstromf1313152011-01-03 14:44:55 +01001615 struct net *net;
Julius Volz51ef3482008-09-02 15:55:40 +02001616 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001618 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 struct ip_vs_conn *cp;
Simon Horman4a516f12011-09-16 14:11:49 +09001620 int ret, pkts;
Hans Schillstromf1313152011-01-03 14:44:55 +01001621 struct netns_ipvs *ipvs;
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001622 int conn_reuse_mode;
Julius Volz51ef3482008-09-02 15:55:40 +02001623
Julian Anastasovfc604762010-10-17 16:38:15 +03001624 /* Already marked as IPVS request or reply? */
1625 if (skb->ipvs_property)
1626 return NF_ACCEPT;
1627
Julian Anastasovcb591552010-10-17 16:40:51 +03001628 /*
1629 * Big tappo:
1630 * - remote client: only PACKET_HOST
1631 * - route: used for struct net when skb->dev is unset
1632 */
1633 if (unlikely((skb->pkt_type != PACKET_HOST &&
1634 hooknum != NF_INET_LOCAL_OUT) ||
1635 !skb_dst(skb))) {
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001636 ip_vs_fill_iph_skb(af, skb, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001637 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1638 " ignored in hook %u\n",
1639 skb->pkt_type, iph.protocol,
1640 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1641 return NF_ACCEPT;
1642 }
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001643 /* ipvs enabled in this netns ? */
1644 net = skb_net(skb);
Julian Anastasov0c125822013-03-09 23:25:04 +02001645 ipvs = net_ipvs(net);
1646 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001647 return NF_ACCEPT;
1648
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001649 ip_vs_fill_iph_skb(af, skb, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Julian Anastasovcb591552010-10-17 16:40:51 +03001651 /* Bad... Do not break raw sockets */
1652 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1653 af == AF_INET)) {
1654 struct sock *sk = skb->sk;
1655 struct inet_sock *inet = inet_sk(skb->sk);
1656
1657 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1658 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660
Julius Volz94b26552009-08-31 16:22:23 +02001661#ifdef CONFIG_IP_VS_IPV6
1662 if (af == AF_INET6) {
1663 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001664 int related;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001665 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum,
1666 &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Julius Volz94b26552009-08-31 16:22:23 +02001668 if (related)
1669 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001670 }
1671 } else
1672#endif
1673 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001674 int related;
1675 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
Julius Volz94b26552009-08-31 16:22:23 +02001676
1677 if (related)
1678 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 /* Protocol supported? */
Hans Schillstrom93304192011-01-03 14:44:51 +01001682 pd = ip_vs_proto_data_get(net, iph.protocol);
1683 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001685 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 /*
1687 * Check if the packet belongs to an existing connection entry
1688 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001689 cp = pp->conn_in_get(af, skb, &iph, 0);
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001690
Marcelo Ricardo Leitnerd752c362015-02-23 15:02:34 -03001691 conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
1692 if (conn_reuse_mode && !iph.fragoffs &&
1693 is_new_conn(skb, &iph) && cp &&
1694 ((unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest &&
1695 unlikely(!atomic_read(&cp->dest->weight))) ||
1696 unlikely(is_new_conn_expected(cp, conn_reuse_mode)))) {
1697 if (!atomic_read(&cp->n_control))
1698 ip_vs_conn_expire_now(cp);
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001699 __ip_vs_conn_put(cp);
1700 cp = NULL;
1701 }
1702
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001703 if (unlikely(!cp) && !iph.fragoffs) {
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001704 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1705 * replayed fragment zero will already have created the cp
1706 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 int v;
1708
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001709 /* Schedule and create new connection entry into &cp */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001710 if (!pp->conn_schedule(af, skb, pd, &v, &cp, &iph))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return v;
1712 }
1713
1714 if (unlikely(!cp)) {
1715 /* sorry, all this trouble for a no-hit :) */
Julian Anastasov0d796412010-10-17 16:46:17 +03001716 IP_VS_DBG_PKT(12, af, pp, skb, 0,
Julian Anastasovcb591552010-10-17 16:40:51 +03001717 "ip_vs_in: packet continues traversal as normal");
Jiri Pirko6aafeef2013-11-06 17:52:20 +01001718 if (iph.fragoffs) {
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001719 /* Fragment that couldn't be mapped to a conn entry
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001720 * is missing module nf_defrag_ipv6
1721 */
1722 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1723 IP_VS_DBG_PKT(7, af, pp, skb, 0, "unhandled fragment");
1724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return NF_ACCEPT;
1726 }
1727
Julian Anastasov0d796412010-10-17 16:46:17 +03001728 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 /* Check the server status */
1730 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1731 /* the destination server is not available */
1732
Simon Horman71a8ab62011-02-04 18:33:01 +09001733 if (sysctl_expire_nodest_conn(ipvs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /* try to expire the connection immediately */
1735 ip_vs_conn_expire_now(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
Julian Anastasovdc8103f2005-11-08 09:40:05 -08001737 /* don't restart its timer, and silently
1738 drop the packet. */
1739 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return NF_DROP;
1741 }
1742
1743 ip_vs_in_stats(cp, skb);
Simon Horman4a516f12011-09-16 14:11:49 +09001744 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (cp->packet_xmit)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001746 ret = cp->packet_xmit(skb, cp, pp, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /* do not touch skb anymore */
1748 else {
1749 IP_VS_DBG_RL("warning: packet_xmit is null");
1750 ret = NF_ACCEPT;
1751 }
1752
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001753 /* Increase its packet counter and check if it is needed
1754 * to be synchronized
1755 *
1756 * Sync connection if it is about to close to
1757 * encorage the standby servers to update the connections timeout
Hans Schillstrom986a0752010-11-19 14:25:13 +01001758 *
1759 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001760 */
Hans Schillstromf1313152011-01-03 14:44:55 +01001761
Hans Schillstrom986a0752010-11-19 14:25:13 +01001762 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
Simon Horman59e03502011-02-04 18:33:01 +09001763 pkts = sysctl_sync_threshold(ipvs);
Hans Schillstrom986a0752010-11-19 14:25:13 +01001764 else
1765 pkts = atomic_add_return(1, &cp->in_pkts);
1766
Julian Anastasov749c42b2012-04-24 23:46:40 +03001767 if (ipvs->sync_state & IP_VS_STATE_MASTER)
1768 ip_vs_sync_conn(net, cp, pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 ip_vs_conn_put(cp);
1771 return ret;
1772}
1773
Julian Anastasovcb591552010-10-17 16:40:51 +03001774/*
1775 * AF_INET handler in NF_INET_LOCAL_IN chain
1776 * Schedule and forward packets from remote clients
1777 */
1778static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001779ip_vs_remote_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001780 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03001781{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001782 return ip_vs_in(ops->hooknum, skb, AF_INET);
Julian Anastasovcb591552010-10-17 16:40:51 +03001783}
1784
1785/*
1786 * AF_INET handler in NF_INET_LOCAL_OUT chain
1787 * Schedule and forward packets from local clients
1788 */
1789static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001790ip_vs_local_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001791 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03001792{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001793 return ip_vs_in(ops->hooknum, skb, AF_INET);
Julian Anastasovcb591552010-10-17 16:40:51 +03001794}
1795
1796#ifdef CONFIG_IP_VS_IPV6
1797
1798/*
1799 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1800 * Schedule and forward packets from remote clients
1801 */
1802static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001803ip_vs_remote_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001804 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03001805{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001806 return ip_vs_in(ops->hooknum, skb, AF_INET6);
Julian Anastasovcb591552010-10-17 16:40:51 +03001807}
1808
1809/*
1810 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1811 * Schedule and forward packets from local clients
1812 */
1813static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001814ip_vs_local_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001815 const struct nf_hook_state *state)
Julian Anastasovcb591552010-10-17 16:40:51 +03001816{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001817 return ip_vs_in(ops->hooknum, skb, AF_INET6);
Julian Anastasovcb591552010-10-17 16:40:51 +03001818}
1819
1820#endif
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823/*
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001824 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 * related packets destined for 0.0.0.0/0.
1826 * When fwmark-based virtual service is used, such as transparent
1827 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1828 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001829 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 * and send them to ip_vs_in_icmp.
1831 */
1832static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001833ip_vs_forward_icmp(const struct nf_hook_ops *ops, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001834 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
1836 int r;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001837 struct net *net;
Julian Anastasov0c125822013-03-09 23:25:04 +02001838 struct netns_ipvs *ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Herbert Xu3db05fe2007-10-15 00:53:15 -07001840 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return NF_ACCEPT;
1842
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001843 /* ipvs enabled in this netns ? */
1844 net = skb_net(skb);
Julian Anastasov0c125822013-03-09 23:25:04 +02001845 ipvs = net_ipvs(net);
1846 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001847 return NF_ACCEPT;
1848
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001849 return ip_vs_in_icmp(skb, &r, ops->hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
Julius Volz2a3b7912008-09-02 15:55:47 +02001852#ifdef CONFIG_IP_VS_IPV6
1853static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001854ip_vs_forward_icmp_v6(const struct nf_hook_ops *ops, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04001855 const struct nf_hook_state *state)
Julius Volz2a3b7912008-09-02 15:55:47 +02001856{
1857 int r;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001858 struct net *net;
Julian Anastasov0c125822013-03-09 23:25:04 +02001859 struct netns_ipvs *ipvs;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001860 struct ip_vs_iphdr iphdr;
Julius Volz2a3b7912008-09-02 15:55:47 +02001861
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001862 ip_vs_fill_iph_skb(AF_INET6, skb, &iphdr);
1863 if (iphdr.protocol != IPPROTO_ICMPV6)
Julius Volz2a3b7912008-09-02 15:55:47 +02001864 return NF_ACCEPT;
1865
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001866 /* ipvs enabled in this netns ? */
1867 net = skb_net(skb);
Julian Anastasov0c125822013-03-09 23:25:04 +02001868 ipvs = net_ipvs(net);
1869 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001870 return NF_ACCEPT;
1871
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001872 return ip_vs_in_icmp_v6(skb, &r, ops->hooknum, &iphdr);
Julius Volz2a3b7912008-09-02 15:55:47 +02001873}
1874#endif
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Patrick McHardy19994142007-12-05 01:23:00 -08001877static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
Julian Anastasovcb591552010-10-17 16:40:51 +03001878 /* After packet filtering, change source only for VS/NAT */
1879 {
1880 .hook = ip_vs_reply4,
1881 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001882 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001883 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001884 .priority = NF_IP_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001885 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001886 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1887 * or VS/NAT(change destination), so that filtering rules can be
1888 * applied to IPVS. */
1889 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001890 .hook = ip_vs_remote_request4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001891 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001892 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001893 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001894 .priority = NF_IP_PRI_NAT_SRC - 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001895 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001896 /* Before ip_vs_in, change source only for VS/NAT */
Patrick McHardy41c5b312007-12-05 01:22:43 -08001897 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001898 .hook = ip_vs_local_reply4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001899 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001900 .pf = NFPROTO_IPV4,
Julian Anastasovfc604762010-10-17 16:38:15 +03001901 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001902 .priority = NF_IP_PRI_NAT_DST + 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001903 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001904 /* After mangle, schedule and forward local requests */
1905 {
1906 .hook = ip_vs_local_request4,
1907 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001908 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001909 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001910 .priority = NF_IP_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001911 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001912 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1913 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1914 {
1915 .hook = ip_vs_forward_icmp,
1916 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001917 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001918 .hooknum = NF_INET_FORWARD,
1919 .priority = 99,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001920 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001921 /* After packet filtering, change source only for VS/NAT */
1922 {
1923 .hook = ip_vs_reply4,
1924 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001925 .pf = NFPROTO_IPV4,
Julian Anastasovfc604762010-10-17 16:38:15 +03001926 .hooknum = NF_INET_FORWARD,
1927 .priority = 100,
1928 },
Julius Volz473b23d2008-09-02 15:55:54 +02001929#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001930 /* After packet filtering, change source only for VS/NAT */
1931 {
1932 .hook = ip_vs_reply6,
1933 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001934 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001935 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001936 .priority = NF_IP6_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001937 },
Julius Volz473b23d2008-09-02 15:55:54 +02001938 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1939 * or VS/NAT(change destination), so that filtering rules can be
1940 * applied to IPVS. */
1941 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001942 .hook = ip_vs_remote_request6,
Julius Volz473b23d2008-09-02 15:55:54 +02001943 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001944 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001945 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001946 .priority = NF_IP6_PRI_NAT_SRC - 1,
Julius Volz473b23d2008-09-02 15:55:54 +02001947 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001948 /* Before ip_vs_in, change source only for VS/NAT */
Julius Volz473b23d2008-09-02 15:55:54 +02001949 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001950 .hook = ip_vs_local_reply6,
Julius Volz473b23d2008-09-02 15:55:54 +02001951 .owner = THIS_MODULE,
Julian Anastasoveb90b0c2014-08-22 17:53:41 +03001952 .pf = NFPROTO_IPV6,
Julian Anastasovfc604762010-10-17 16:38:15 +03001953 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001954 .priority = NF_IP6_PRI_NAT_DST + 1,
Julius Volz473b23d2008-09-02 15:55:54 +02001955 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001956 /* After mangle, schedule and forward local requests */
1957 {
1958 .hook = ip_vs_local_request6,
1959 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001960 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001961 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001962 .priority = NF_IP6_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001963 },
Julius Volz473b23d2008-09-02 15:55:54 +02001964 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1965 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1966 {
1967 .hook = ip_vs_forward_icmp_v6,
1968 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001969 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001970 .hooknum = NF_INET_FORWARD,
1971 .priority = 99,
Julius Volz473b23d2008-09-02 15:55:54 +02001972 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001973 /* After packet filtering, change source only for VS/NAT */
1974 {
1975 .hook = ip_vs_reply6,
1976 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001977 .pf = NFPROTO_IPV6,
Julian Anastasovfc604762010-10-17 16:38:15 +03001978 .hooknum = NF_INET_FORWARD,
1979 .priority = 100,
1980 },
Julius Volz473b23d2008-09-02 15:55:54 +02001981#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982};
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001983/*
1984 * Initialize IP Virtual Server netns mem.
1985 */
1986static int __net_init __ip_vs_init(struct net *net)
1987{
1988 struct netns_ipvs *ipvs;
1989
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001990 ipvs = net_generic(net, ip_vs_net_id);
Joe Perches0a9ee812011-08-29 14:17:25 -07001991 if (ipvs == NULL)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001992 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -07001993
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001994 /* Hold the beast until a service is registerd */
1995 ipvs->enable = 0;
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001996 ipvs->net = net;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001997 /* Counters used for creating unique names */
1998 ipvs->gen = atomic_read(&ipvs_netns_cnt);
1999 atomic_inc(&ipvs_netns_cnt);
2000 net->ipvs = ipvs;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002001
Hans Schillstrom503cf152011-05-01 18:50:16 +02002002 if (ip_vs_estimator_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002003 goto estimator_fail;
2004
Hans Schillstrom503cf152011-05-01 18:50:16 +02002005 if (ip_vs_control_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002006 goto control_fail;
2007
Hans Schillstrom503cf152011-05-01 18:50:16 +02002008 if (ip_vs_protocol_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002009 goto protocol_fail;
2010
Hans Schillstrom503cf152011-05-01 18:50:16 +02002011 if (ip_vs_app_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002012 goto app_fail;
2013
Hans Schillstrom503cf152011-05-01 18:50:16 +02002014 if (ip_vs_conn_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002015 goto conn_fail;
2016
Hans Schillstrom503cf152011-05-01 18:50:16 +02002017 if (ip_vs_sync_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002018 goto sync_fail;
2019
Simon Hormana870c8c2011-02-01 18:21:53 +01002020 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002021 sizeof(struct netns_ipvs), ipvs->gen);
2022 return 0;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002023/*
2024 * Error handling
2025 */
2026
2027sync_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002028 ip_vs_conn_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002029conn_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002030 ip_vs_app_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002031app_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002032 ip_vs_protocol_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002033protocol_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002034 ip_vs_control_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002035control_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002036 ip_vs_estimator_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002037estimator_fail:
Julian Anastasov39f618b2012-04-25 00:29:58 +03002038 net->ipvs = NULL;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002039 return -ENOMEM;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002040}
2041
2042static void __net_exit __ip_vs_cleanup(struct net *net)
2043{
Hans Schillstrom503cf152011-05-01 18:50:16 +02002044 ip_vs_service_net_cleanup(net); /* ip_vs_flush() with locks */
2045 ip_vs_conn_net_cleanup(net);
2046 ip_vs_app_net_cleanup(net);
2047 ip_vs_protocol_net_cleanup(net);
2048 ip_vs_control_net_cleanup(net);
2049 ip_vs_estimator_net_cleanup(net);
Hans Schillstrom1ae132b2011-05-03 22:09:30 +02002050 IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net)->gen);
Julian Anastasov39f618b2012-04-25 00:29:58 +03002051 net->ipvs = NULL;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002052}
2053
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002054static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2055{
2056 EnterFunction(2);
2057 net_ipvs(net)->enable = 0; /* Disable packet reception */
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +02002058 smp_wmb();
Hans Schillstrom503cf152011-05-01 18:50:16 +02002059 ip_vs_sync_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002060 LeaveFunction(2);
2061}
2062
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002063static struct pernet_operations ipvs_core_ops = {
2064 .init = __ip_vs_init,
2065 .exit = __ip_vs_cleanup,
2066 .id = &ip_vs_net_id,
2067 .size = sizeof(struct netns_ipvs),
2068};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002070static struct pernet_operations ipvs_core_dev_ops = {
2071 .exit = __ip_vs_dev_cleanup,
2072};
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074/*
2075 * Initialize IP Virtual Server
2076 */
2077static int __init ip_vs_init(void)
2078{
2079 int ret;
2080
2081 ret = ip_vs_control_init();
2082 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002083 pr_err("can't setup control.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002084 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
2086
2087 ip_vs_protocol_init();
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 ret = ip_vs_conn_init();
2090 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002091 pr_err("can't setup connection table.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002092 goto cleanup_protocol;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002093 }
2094
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002095 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2096 if (ret < 0)
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002097 goto cleanup_conn;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002098
2099 ret = register_pernet_device(&ipvs_core_dev_ops);
2100 if (ret < 0)
2101 goto cleanup_sub;
2102
Patrick McHardy41c5b312007-12-05 01:22:43 -08002103 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002105 pr_err("can't register hooks.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002106 goto cleanup_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108
Hans Schillstrom8537de82012-04-26 07:47:44 +02002109 ret = ip_vs_register_nl_ioctl();
2110 if (ret < 0) {
2111 pr_err("can't register netlink/ioctl.\n");
2112 goto cleanup_hooks;
2113 }
2114
Hannes Eder1e3e2382009-08-02 11:05:41 +00002115 pr_info("ipvs loaded.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return ret;
2118
Hans Schillstrom8537de82012-04-26 07:47:44 +02002119cleanup_hooks:
2120 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002121cleanup_dev:
2122 unregister_pernet_device(&ipvs_core_dev_ops);
2123cleanup_sub:
2124 unregister_pernet_subsys(&ipvs_core_ops);
Hans Schillstrom552ad652011-06-13 12:19:26 +02002125cleanup_conn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 ip_vs_conn_cleanup();
Hans Schillstrom552ad652011-06-13 12:19:26 +02002127cleanup_protocol:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 ip_vs_protocol_cleanup();
2129 ip_vs_control_cleanup();
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002130exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return ret;
2132}
2133
2134static void __exit ip_vs_cleanup(void)
2135{
Hans Schillstrom8537de82012-04-26 07:47:44 +02002136 ip_vs_unregister_nl_ioctl();
Patrick McHardy41c5b312007-12-05 01:22:43 -08002137 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002138 unregister_pernet_device(&ipvs_core_dev_ops);
2139 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 ip_vs_conn_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 ip_vs_protocol_cleanup();
2142 ip_vs_control_cleanup();
Hannes Eder1e3e2382009-08-02 11:05:41 +00002143 pr_info("ipvs unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146module_init(ip_vs_init);
2147module_exit(ip_vs_cleanup);
2148MODULE_LICENSE("GPL");