blob: 5c34e8d42e0190a14ec31c1238bde5929fdf0539 [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);
122 s->ustats.inpkts++;
123 u64_stats_update_begin(&s->syncp);
124 s->ustats.inbytes += skb->len;
125 u64_stats_update_end(&s->syncp);
Linus 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 s->ustats.inpkts++;
131 u64_stats_update_begin(&s->syncp);
132 s->ustats.inbytes += skb->len;
133 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 s->ustats.inpkts++;
138 u64_stats_update_begin(&s->syncp);
139 s->ustats.inbytes += skb->len;
140 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);
156 s->ustats.outpkts++;
157 u64_stats_update_begin(&s->syncp);
158 s->ustats.outbytes += skb->len;
159 u64_stats_update_end(&s->syncp);
Linus 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 s->ustats.outpkts++;
165 u64_stats_update_begin(&s->syncp);
166 s->ustats.outbytes += skb->len;
167 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 s->ustats.outpkts++;
172 u64_stats_update_begin(&s->syncp);
173 s->ustats.outbytes += skb->len;
174 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);
186 s->ustats.conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Hans Schillstromb17fc992011-01-03 14:44:56 +0100188 s = this_cpu_ptr(svc->stats.cpustats);
189 s->ustats.conns++;
190
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200191 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100192 s->ustats.conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195
Simon Horman4a516f12011-09-16 14:11:49 +0900196static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197ip_vs_set_state(struct ip_vs_conn *cp, int direction,
198 const struct sk_buff *skb,
Hans Schillstrom93304192011-01-03 14:44:51 +0100199 struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Simon Horman4a516f12011-09-16 14:11:49 +0900201 if (likely(pd->pp->state_transition))
202 pd->pp->state_transition(cp, direction, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Hans Schillstroma5959d52010-11-19 14:25:10 +0100205static inline int
Simon Horman85999282010-08-22 21:37:53 +0900206ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
207 struct sk_buff *skb, int protocol,
208 const union nf_inet_addr *caddr, __be16 cport,
209 const union nf_inet_addr *vaddr, __be16 vport,
210 struct ip_vs_conn_param *p)
211{
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100212 ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr,
213 vport, p);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200214 p->pe = rcu_dereference(svc->pe);
Simon Horman85999282010-08-22 21:37:53 +0900215 if (p->pe && p->pe->fill_param)
Hans Schillstroma5959d52010-11-19 14:25:10 +0100216 return p->pe->fill_param(p, skb);
217
218 return 0;
Simon Horman85999282010-08-22 21:37:53 +0900219}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
222 * IPVS persistent scheduling function
223 * It creates a connection entry according to its template if exists,
224 * or selects a server and creates a connection entry plus a template.
225 * Locking: we are svc user (svc->refcnt), so we hold all dests too
226 * Protocols supported: TCP, UDP
227 */
228static struct ip_vs_conn *
229ip_vs_sched_persist(struct ip_vs_service *svc,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200230 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
231 int *ignored, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct ip_vs_conn *cp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 struct ip_vs_dest *dest;
235 struct ip_vs_conn *ct;
Simon Horman5b57a982010-08-22 21:37:51 +0900236 __be16 dport = 0; /* destination port to forward */
Julian Anastasov35757922010-09-17 14:18:16 +0200237 unsigned int flags;
Simon Hormanf11017e2010-08-22 21:37:52 +0900238 struct ip_vs_conn_param param;
Simon Hormane0aac522012-01-27 10:45:27 +0900239 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
Julius Volz28364a52008-09-02 15:55:43 +0200240 union nf_inet_addr snet; /* source network of the client,
241 after masking */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* Mask saddr with the netmask to adjust template granularity */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200244#ifdef CONFIG_IP_VS_IPV6
245 if (svc->af == AF_INET6)
Julian Anastasov0a925862013-04-17 23:50:49 +0300246 ipv6_addr_prefix(&snet.in6, &iph->saddr.in6,
247 (__force __u32) svc->netmask);
Julius Volzcd17f9e2008-09-02 15:55:46 +0200248 else
249#endif
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200250 snet.ip = iph->saddr.ip & svc->netmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Julius Volzcd17f9e2008-09-02 15:55:46 +0200252 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
253 "mnet %s\n",
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200254 IP_VS_DBG_ADDR(svc->af, &iph->saddr), ntohs(src_port),
255 IP_VS_DBG_ADDR(svc->af, &iph->daddr), ntohs(dst_port),
Julius Volzcd17f9e2008-09-02 15:55:46 +0200256 IP_VS_DBG_ADDR(svc->af, &snet));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /*
259 * As far as we know, FTP is a very complicated network protocol, and
260 * it uses control connection and data connections. For active FTP,
261 * FTP server initialize data connection to the client, its source port
262 * is often 20. For passive FTP, FTP server tells the clients the port
263 * that it passively listens to, and the client issues the data
264 * connection. In the tunneling or direct routing mode, the load
265 * balancer is on the client-to-server half of connection, the port
266 * number is unknown to the load balancer. So, a conn template like
267 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
268 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
269 * is created for other persistent services.
270 */
Simon Horman5b57a982010-08-22 21:37:51 +0900271 {
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200272 int protocol = iph->protocol;
273 const union nf_inet_addr *vaddr = &iph->daddr;
Simon Hormanf11017e2010-08-22 21:37:52 +0900274 __be16 vport = 0;
275
Hans Schillstromce144f22010-11-19 14:25:08 +0100276 if (dst_port == svc->port) {
Simon Horman5b57a982010-08-22 21:37:51 +0900277 /* non-FTP template:
278 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
279 * FTP template:
280 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
282 if (svc->port != FTPPORT)
Hans Schillstromce144f22010-11-19 14:25:08 +0100283 vport = dst_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900285 /* Note: persistent fwmark-based services and
286 * persistent port zero service are handled here.
287 * fwmark template:
288 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
289 * port zero template:
290 * <protocol,caddr,0,vaddr,0,daddr,0>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
Julius Volz28364a52008-09-02 15:55:43 +0200292 if (svc->fwmark) {
Simon Horman5b57a982010-08-22 21:37:51 +0900293 protocol = IPPROTO_IP;
294 vaddr = &fwmark;
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Hans Schillstroma5959d52010-11-19 14:25:10 +0100297 /* return *ignored = -1 so NF_DROP can be used */
298 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
299 vaddr, vport, &param) < 0) {
300 *ignored = -1;
301 return NULL;
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
Simon Horman5b57a982010-08-22 21:37:51 +0900305 /* Check if a template already exists */
Simon Hormanf11017e2010-08-22 21:37:52 +0900306 ct = ip_vs_ct_in_get(&param);
Simon Horman5b57a982010-08-22 21:37:51 +0900307 if (!ct || !ip_vs_check_template(ct)) {
Julian Anastasovceec4c32013-03-22 11:46:53 +0200308 struct ip_vs_scheduler *sched;
309
Hans Schillstroma5959d52010-11-19 14:25:10 +0100310 /*
311 * No template found or the dest of the connection
Simon Horman5b57a982010-08-22 21:37:51 +0900312 * template is not available.
Hans Schillstroma5959d52010-11-19 14:25:10 +0100313 * return *ignored=0 i.e. ICMP and NF_DROP
Simon Horman5b57a982010-08-22 21:37:51 +0900314 */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200315 sched = rcu_dereference(svc->scheduler);
Julian Anastasovbba54de2013-06-16 09:09:36 +0300316 dest = sched->schedule(svc, skb, iph);
Simon Horman5b57a982010-08-22 21:37:51 +0900317 if (!dest) {
318 IP_VS_DBG(1, "p-schedule: no dest found.\n");
Simon Horman85999282010-08-22 21:37:53 +0900319 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100320 *ignored = 0;
Simon Horman5b57a982010-08-22 21:37:51 +0900321 return NULL;
322 }
323
Hans Schillstromce144f22010-11-19 14:25:08 +0100324 if (dst_port == svc->port && svc->port != FTPPORT)
Simon Horman5b57a982010-08-22 21:37:51 +0900325 dport = dest->port;
326
Simon Horman85999282010-08-22 21:37:53 +0900327 /* Create a template
328 * This adds param.pe_data to the template,
329 * and thus param.pe_data will be destroyed
330 * when the template expires */
Simon Hormanf11017e2010-08-22 21:37:52 +0900331 ct = ip_vs_conn_new(&param, &dest->addr, dport,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100332 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
Simon Horman85999282010-08-22 21:37:53 +0900333 if (ct == NULL) {
334 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100335 *ignored = -1;
Simon Horman5b57a982010-08-22 21:37:51 +0900336 return NULL;
Simon Horman85999282010-08-22 21:37:53 +0900337 }
Simon Horman5b57a982010-08-22 21:37:51 +0900338
339 ct->timeout = svc->timeout;
Simon Horman85999282010-08-22 21:37:53 +0900340 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900341 /* set destination with the found template */
342 dest = ct->dest;
Simon Horman85999282010-08-22 21:37:53 +0900343 kfree(param.pe_data);
344 }
Simon Horman5b57a982010-08-22 21:37:51 +0900345
Hans Schillstromce144f22010-11-19 14:25:08 +0100346 dport = dst_port;
Simon Horman5b57a982010-08-22 21:37:51 +0900347 if (dport == svc->port && dest->port)
348 dport = dest->port;
349
Nick Chalk26ec0372010-06-22 08:07:01 +0200350 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200351 && iph->protocol == IPPROTO_UDP) ?
Nick Chalk26ec0372010-06-22 08:07:01 +0200352 IP_VS_CONN_F_ONE_PACKET : 0;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /*
355 * Create a new connection according to the template
356 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200357 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol, &iph->saddr,
358 src_port, &iph->daddr, dst_port, &param);
Hans Schillstromce144f22010-11-19 14:25:08 +0100359
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100360 cp = ip_vs_conn_new(&param, &dest->addr, dport, flags, dest, skb->mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (cp == NULL) {
362 ip_vs_conn_put(ct);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100363 *ignored = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return NULL;
365 }
366
367 /*
368 * Add its control
369 */
370 ip_vs_control_add(cp, ct);
371 ip_vs_conn_put(ct);
372
373 ip_vs_conn_stats(cp, svc);
374 return cp;
375}
376
377
378/*
379 * IPVS main scheduling function
380 * It selects a server according to the virtual service, and
381 * creates a connection entry.
382 * Protocols supported: TCP, UDP
Hans Schillstroma5959d52010-11-19 14:25:10 +0100383 *
384 * Usage of *ignored
385 *
386 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
387 * svc/scheduler decides that this packet should be accepted with
388 * NF_ACCEPT because it must not be scheduled.
389 *
390 * 0 : scheduler can not find destination, so try bypass or
391 * return ICMP and then NF_DROP (ip_vs_leave).
392 *
393 * -1 : scheduler tried to schedule but fatal error occurred, eg.
394 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
395 * failure such as missing Call-ID, ENOMEM on skb_linearize
396 * or pe_data. In this case we should return NF_DROP without
397 * any attempts to send ICMP with ip_vs_leave.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
399struct ip_vs_conn *
Julian Anastasov190ecd22010-10-17 16:24:37 +0300400ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200401 struct ip_vs_proto_data *pd, int *ignored,
402 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Hans Schillstrom93304192011-01-03 14:44:51 +0100404 struct ip_vs_protocol *pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct ip_vs_conn *cp = NULL;
Julian Anastasovceec4c32013-03-22 11:46:53 +0200406 struct ip_vs_scheduler *sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct ip_vs_dest *dest;
Julian Anastasov35757922010-09-17 14:18:16 +0200408 __be16 _ports[2], *pptr;
409 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Julian Anastasov190ecd22010-10-17 16:24:37 +0300411 *ignored = 1;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200412 /*
413 * IPv6 frags, only the first hit here.
414 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200415 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (pptr == NULL)
417 return NULL;
418
419 /*
Julian Anastasov190ecd22010-10-17 16:24:37 +0300420 * FTPDATA needs this check when using local real server.
421 * Never schedule Active FTPDATA connections from real server.
422 * For LVS-NAT they must be already created. For other methods
423 * with persistence the connection is created on SYN+ACK.
424 */
425 if (pptr[0] == FTPDATA) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300426 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
427 "Not scheduling FTPDATA");
Julian Anastasov190ecd22010-10-17 16:24:37 +0300428 return NULL;
429 }
430
431 /*
Hans Schillstroma5959d52010-11-19 14:25:10 +0100432 * Do not schedule replies from local real server.
Julian Anastasov190ecd22010-10-17 16:24:37 +0300433 */
434 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200435 (cp = pp->conn_in_get(svc->af, skb, iph, 1))) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300436 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
Julian Anastasov190ecd22010-10-17 16:24:37 +0300437 "Not scheduling reply for existing connection");
438 __ip_vs_conn_put(cp);
439 return NULL;
440 }
441
442 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * Persistent service
444 */
Hans Schillstroma5959d52010-11-19 14:25:10 +0100445 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200446 return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored,
447 iph);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100448
449 *ignored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /*
452 * Non-persistent service
453 */
454 if (!svc->fwmark && pptr[1] != svc->port) {
455 if (!svc->port)
Hannes Eder1e3e2382009-08-02 11:05:41 +0000456 pr_err("Schedule: port zero only supported "
457 "in persistent services, "
458 "check your ipvs configuration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return NULL;
460 }
461
Julian Anastasovceec4c32013-03-22 11:46:53 +0200462 sched = rcu_dereference(svc->scheduler);
Julian Anastasovbba54de2013-06-16 09:09:36 +0300463 dest = sched->schedule(svc, skb, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (dest == NULL) {
465 IP_VS_DBG(1, "Schedule: no dest found.\n");
466 return NULL;
467 }
468
Nick Chalk26ec0372010-06-22 08:07:01 +0200469 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200470 && iph->protocol == IPPROTO_UDP) ?
Nick Chalk26ec0372010-06-22 08:07:01 +0200471 IP_VS_CONN_F_ONE_PACKET : 0;
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /*
474 * Create a connection entry.
475 */
Simon Hormanf11017e2010-08-22 21:37:52 +0900476 {
477 struct ip_vs_conn_param p;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100478
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200479 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
480 &iph->saddr, pptr[0], &iph->daddr,
481 pptr[1], &p);
Simon Hormanf11017e2010-08-22 21:37:52 +0900482 cp = ip_vs_conn_new(&p, &dest->addr,
483 dest->port ? dest->port : pptr[1],
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100484 flags, dest, skb->mark);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100485 if (!cp) {
486 *ignored = -1;
Simon Hormanf11017e2010-08-22 21:37:52 +0900487 return NULL;
Hans Schillstroma5959d52010-11-19 14:25:10 +0100488 }
Simon Hormanf11017e2010-08-22 21:37:52 +0900489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Julius Volzcd17f9e2008-09-02 15:55:46 +0200491 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
492 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
493 ip_vs_fwd_tag(cp),
494 IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
495 IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
496 IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
497 cp->flags, atomic_read(&cp->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 ip_vs_conn_stats(cp, svc);
500 return cp;
501}
502
503
504/*
505 * Pass or drop the packet.
506 * Called by ip_vs_in, when the virtual service is available but
507 * no destination is available for a new connection.
508 */
509int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200510 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Al Viro014d7302006-09-28 14:29:52 -0700512 __be16 _ports[2], *pptr;
Simon Hormana7a86b82011-02-04 18:33:02 +0900513#ifdef CONFIG_SYSCTL
514 struct net *net;
515 struct netns_ipvs *ipvs;
Julius Volz2a3b7912008-09-02 15:55:47 +0200516 int unicast;
Simon Hormana7a86b82011-02-04 18:33:02 +0900517#endif
Hans Schillstrom93304192011-01-03 14:44:51 +0100518
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200519 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (pptr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return NF_DROP;
522 }
Simon Hormana7a86b82011-02-04 18:33:02 +0900523
524#ifdef CONFIG_SYSCTL
Hans Schillstrom4a984802011-01-03 14:45:02 +0100525 net = skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Julius Volz2a3b7912008-09-02 15:55:47 +0200527#ifdef CONFIG_IP_VS_IPV6
528 if (svc->af == AF_INET6)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200529 unicast = ipv6_addr_type(&iph->daddr.in6) & IPV6_ADDR_UNICAST;
Julius Volz2a3b7912008-09-02 15:55:47 +0200530 else
531#endif
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200532 unicast = (inet_addr_type(net, iph->daddr.ip) == RTN_UNICAST);
Julius Volz2a3b7912008-09-02 15:55:47 +0200533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* if it is fwmark-based service, the cache_bypass sysctl is up
Julius Volz2a3b7912008-09-02 15:55:47 +0200535 and the destination is a non-local unicast, then create
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 a cache_bypass connection entry */
Hans Schillstrom4a984802011-01-03 14:45:02 +0100537 ipvs = net_ipvs(net);
Hans Schillstroma0840e22011-01-03 14:44:58 +0100538 if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) {
Krzysztof Wilczynskiad542ced2011-10-18 20:59:49 +0100539 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct ip_vs_conn *cp;
Julian Anastasov35757922010-09-17 14:18:16 +0200541 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200542 iph->protocol == IPPROTO_UDP) ?
Julian Anastasov35757922010-09-17 14:18:16 +0200543 IP_VS_CONN_F_ONE_PACKET : 0;
Simon Hormandff630d2008-09-17 10:10:42 +1000544 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* create a new connection entry */
Hannes Eder1e3e2382009-08-02 11:05:41 +0000547 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
Simon Hormanf11017e2010-08-22 21:37:52 +0900548 {
549 struct ip_vs_conn_param p;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200550 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
551 &iph->saddr, pptr[0],
552 &iph->daddr, pptr[1], &p);
Simon Hormanf11017e2010-08-22 21:37:52 +0900553 cp = ip_vs_conn_new(&p, &daddr, 0,
554 IP_VS_CONN_F_BYPASS | flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100555 NULL, skb->mark);
Simon Hormanf11017e2010-08-22 21:37:52 +0900556 if (!cp)
557 return NF_DROP;
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* statistics */
561 ip_vs_in_stats(cp, skb);
562
563 /* set state */
Simon Horman4a516f12011-09-16 14:11:49 +0900564 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* transmit the first SYN packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200567 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* do not touch skb anymore */
569
570 atomic_inc(&cp->in_pkts);
571 ip_vs_conn_put(cp);
572 return ret;
573 }
Simon Hormana7a86b82011-02-04 18:33:02 +0900574#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /*
577 * When the virtual ftp service is presented, packets destined
578 * for other services on the VIP may get here (except services
579 * listed in the ipvs table), pass the packets, because it is
580 * not ipvs job to decide to drop the packets.
581 */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200582 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /*
586 * Notify the client that the destination is unreachable, and
587 * release the socket buffer.
588 * Since it is in IP layer, the TCP socket is not actually
589 * created, the TCP RST packet cannot be sent, instead that
590 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
591 */
Julius Volz2a3b7912008-09-02 15:55:47 +0200592#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +0300593 if (svc->af == AF_INET6) {
594 if (!skb->dev) {
Simon Horman9fd0fa72013-04-19 10:25:42 +0900595 struct net *net_ = dev_net(skb_dst(skb)->dev);
Julian Anastasovcb591552010-10-17 16:40:51 +0300596
Simon Horman9fd0fa72013-04-19 10:25:42 +0900597 skb->dev = net_->loopback_dev;
Julian Anastasovcb591552010-10-17 16:40:51 +0300598 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000599 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Julian Anastasovcb591552010-10-17 16:40:51 +0300600 } else
Julius Volz2a3b7912008-09-02 15:55:47 +0200601#endif
602 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return NF_DROP;
605}
606
Simon Horman84b3cee2011-02-04 18:33:01 +0900607#ifdef CONFIG_SYSCTL
608
609static int sysctl_snat_reroute(struct sk_buff *skb)
610{
611 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
612 return ipvs->sysctl_snat_reroute;
613}
614
Simon Horman0cfa5582011-02-04 18:33:01 +0900615static int sysctl_nat_icmp_send(struct net *net)
616{
617 struct netns_ipvs *ipvs = net_ipvs(net);
618 return ipvs->sysctl_nat_icmp_send;
619}
620
Simon Horman71a8ab62011-02-04 18:33:01 +0900621static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
622{
623 return ipvs->sysctl_expire_nodest_conn;
624}
625
Simon Horman84b3cee2011-02-04 18:33:01 +0900626#else
627
628static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; }
Simon Horman0cfa5582011-02-04 18:33:01 +0900629static int sysctl_nat_icmp_send(struct net *net) { return 0; }
Simon Horman71a8ab62011-02-04 18:33:01 +0900630static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
Simon Horman84b3cee2011-02-04 18:33:01 +0900631
632#endif
633
Al Virob1550f22006-11-14 21:37:50 -0800634__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Al Virod3bc23e2006-11-14 21:24:49 -0800636 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300639static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
640{
641 if (NF_INET_LOCAL_IN == hooknum)
642 return IP_DEFRAG_VS_IN;
643 if (NF_INET_FORWARD == hooknum)
644 return IP_DEFRAG_VS_FWD;
645 return IP_DEFRAG_VS_OUT;
646}
647
Herbert Xu776c7292007-10-14 00:38:32 -0700648static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Julian Anastasovac692692013-03-22 11:46:54 +0200650 int err;
Herbert Xu776c7292007-10-14 00:38:32 -0700651
Julian Anastasovac692692013-03-22 11:46:54 +0200652 local_bh_disable();
653 err = ip_defrag(skb, user);
654 local_bh_enable();
Herbert Xu776c7292007-10-14 00:38:32 -0700655 if (!err)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700656 ip_send_check(ip_hdr(skb));
Herbert Xu776c7292007-10-14 00:38:32 -0700657
658 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900661static int ip_vs_route_me_harder(int af, struct sk_buff *skb)
662{
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900663#ifdef CONFIG_IP_VS_IPV6
664 if (af == AF_INET6) {
Simon Horman84b3cee2011-02-04 18:33:01 +0900665 if (sysctl_snat_reroute(skb) && ip6_route_me_harder(skb) != 0)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900666 return 1;
667 } else
668#endif
Simon Horman84b3cee2011-02-04 18:33:01 +0900669 if ((sysctl_snat_reroute(skb) ||
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900670 skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
671 ip_route_me_harder(skb, RTN_LOCAL) != 0)
672 return 1;
673
674 return 0;
675}
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677/*
678 * Packet has been made sufficiently writable in caller
679 * - inout: 1=in->out, 0=out->in
680 */
681void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
682 struct ip_vs_conn *cp, int inout)
683{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700684 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 unsigned int icmp_offset = iph->ihl*4;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700686 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
687 icmp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
689
690 if (inout) {
Julius Volze7ade462008-09-02 15:55:33 +0200691 iph->saddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200693 ciph->daddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 ip_send_check(ciph);
695 } else {
Julius Volze7ade462008-09-02 15:55:33 +0200696 iph->daddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200698 ciph->saddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 ip_send_check(ciph);
700 }
701
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100702 /* the TCP/UDP/SCTP port */
703 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
704 IPPROTO_SCTP == ciph->protocol) {
Al Viro014d7302006-09-28 14:29:52 -0700705 __be16 *ports = (void *)ciph + ciph->ihl*4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 if (inout)
708 ports[1] = cp->vport;
709 else
710 ports[0] = cp->dport;
711 }
712
713 /* And finally the ICMP checksum */
714 icmph->checksum = 0;
715 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
716 skb->ip_summed = CHECKSUM_UNNECESSARY;
717
718 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300719 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 "Forwarding altered outgoing ICMP");
721 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300722 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 "Forwarding altered incoming ICMP");
724}
725
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200726#ifdef CONFIG_IP_VS_IPV6
727void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
728 struct ip_vs_conn *cp, int inout)
729{
730 struct ipv6hdr *iph = ipv6_hdr(skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200731 unsigned int icmp_offset = 0;
732 unsigned int offs = 0; /* header offset*/
733 int protocol;
734 struct icmp6hdr *icmph;
735 struct ipv6hdr *ciph;
736 unsigned short fragoffs;
737
738 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
739 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
740 offs = icmp_offset + sizeof(struct icmp6hdr);
741 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
742
743 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200744
745 if (inout) {
746 iph->saddr = cp->vaddr.in6;
747 ciph->daddr = cp->vaddr.in6;
748 } else {
749 iph->daddr = cp->daddr.in6;
750 ciph->saddr = cp->daddr.in6;
751 }
752
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100753 /* the TCP/UDP/SCTP port */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200754 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
755 IPPROTO_SCTP == protocol)) {
756 __be16 *ports = (void *)(skb_network_header(skb) + offs);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200757
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200758 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
759 ntohs(inout ? ports[1] : ports[0]),
760 ntohs(inout ? cp->vport : cp->dport));
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200761 if (inout)
762 ports[1] = cp->vport;
763 else
764 ports[0] = cp->dport;
765 }
766
767 /* And finally the ICMP checksum */
Simon Horman8870f842010-08-26 13:21:26 -0700768 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
769 skb->len - icmp_offset,
770 IPPROTO_ICMPV6, 0);
771 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
772 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
773 skb->ip_summed = CHECKSUM_PARTIAL;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200774
775 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300776 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
777 (void *)ciph - (void *)iph,
778 "Forwarding altered outgoing ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200779 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300780 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
781 (void *)ciph - (void *)iph,
782 "Forwarding altered incoming ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200783}
784#endif
785
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000786/* Handle relevant response ICMP messages - forward to the right
Julian Anastasov6cb90db2011-02-09 02:26:38 +0200787 * destination host.
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000788 */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000789static int handle_response_icmp(int af, struct sk_buff *skb,
790 union nf_inet_addr *snet,
791 __u8 protocol, struct ip_vs_conn *cp,
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000792 struct ip_vs_protocol *pp,
793 unsigned int offset, unsigned int ihl)
794{
795 unsigned int verdict = NF_DROP;
796
797 if (IP_VS_FWD_METHOD(cp) != 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000798 pr_err("shouldn't reach here, because the box is on the "
799 "half connection in the tun/dr module.\n");
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000800 }
801
802 /* Ensure the checksum is correct */
803 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
804 /* Failed checksum! */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000805 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
806 IP_VS_DBG_ADDR(af, snet));
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000807 goto out;
808 }
809
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100810 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
811 IPPROTO_SCTP == protocol)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000812 offset += 2 * sizeof(__u16);
813 if (!skb_make_writable(skb, offset))
814 goto out;
815
Simon Hormanf2428ed2008-09-05 11:17:14 +1000816#ifdef CONFIG_IP_VS_IPV6
817 if (af == AF_INET6)
818 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
819 else
820#endif
821 ip_vs_nat_icmp(skb, pp, cp, 1);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000822
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900823 if (ip_vs_route_me_harder(af, skb))
824 goto out;
Julian Anastasovf5a41842010-10-17 16:35:46 +0300825
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000826 /* do the statistics and put it back */
827 ip_vs_out_stats(cp, skb);
828
Julian Anastasovcf356d62010-10-17 16:21:07 +0300829 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200830 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +0300831 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200832 else
833 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000834 verdict = NF_ACCEPT;
835
836out:
837 __ip_vs_conn_put(cp);
838
839 return verdict;
840}
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842/*
843 * Handle ICMP messages in the inside-to-outside direction (outgoing).
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000844 * Find any that might be relevant, check against existing connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 * Currently handles error types - unreachable, quench, ttl exceeded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 */
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300847static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
848 unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct iphdr *iph;
851 struct icmphdr _icmph, *ic;
852 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +0200853 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct ip_vs_conn *cp;
855 struct ip_vs_protocol *pp;
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000856 unsigned int offset, ihl;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000857 union nf_inet_addr snet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 *related = 1;
860
861 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -0700862 if (ip_is_fragment(ip_hdr(skb))) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300863 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700867 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 offset = ihl = iph->ihl * 4;
869 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
870 if (ic == NULL)
871 return NF_DROP;
872
Harvey Harrison14d5e832008-10-31 00:54:29 -0700873 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -0700875 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 /*
878 * Work through seeing if this is for us.
879 * These checks are supposed to be in an order that means easy
880 * things are checked first to speed up processing.... however
881 * this means that some packets will manage to get a long way
882 * down this stack and then be rejected, but that's life.
883 */
884 if ((ic->type != ICMP_DEST_UNREACH) &&
885 (ic->type != ICMP_SOURCE_QUENCH) &&
886 (ic->type != ICMP_TIME_EXCEEDED)) {
887 *related = 0;
888 return NF_ACCEPT;
889 }
890
891 /* Now find the contained IP header */
892 offset += sizeof(_icmph);
893 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
894 if (cih == NULL)
895 return NF_ACCEPT; /* The packet looks wrong, ignore */
896
897 pp = ip_vs_proto_get(cih->protocol);
898 if (!pp)
899 return NF_ACCEPT;
900
901 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900902 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 pp->dont_defrag))
904 return NF_ACCEPT;
905
Julian Anastasov0d796412010-10-17 16:46:17 +0300906 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
907 "Checking outgoing ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200909 ip_vs_fill_ip4hdr(cih, &ciph);
910 ciph.len += offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /* The embedded headers contain source and dest in reverse order */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200912 cp = pp->conn_out_get(AF_INET, skb, &ciph, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (!cp)
914 return NF_ACCEPT;
915
Simon Hormanf2428ed2008-09-05 11:17:14 +1000916 snet.ip = iph->saddr;
917 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200918 pp, ciph.len, ihl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Julius Volz2a3b7912008-09-02 15:55:47 +0200921#ifdef CONFIG_IP_VS_IPV6
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300922static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200923 unsigned int hooknum, struct ip_vs_iphdr *ipvsh)
Julius Volz2a3b7912008-09-02 15:55:47 +0200924{
Julius Volz2a3b7912008-09-02 15:55:47 +0200925 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200926 struct ipv6hdr _ip6h, *ip6h; /* The ip header contained within ICMP */
927 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +0200928 struct ip_vs_conn *cp;
929 struct ip_vs_protocol *pp;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000930 union nf_inet_addr snet;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200931 unsigned int writable;
932
Julius Volz2a3b7912008-09-02 15:55:47 +0200933 *related = 1;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200934 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
Julius Volz2a3b7912008-09-02 15:55:47 +0200935 if (ic == NULL)
936 return NF_DROP;
937
Julius Volz2a3b7912008-09-02 15:55:47 +0200938 /*
939 * Work through seeing if this is for us.
940 * These checks are supposed to be in an order that means easy
941 * things are checked first to speed up processing.... however
942 * this means that some packets will manage to get a long way
943 * down this stack and then be rejected, but that's life.
944 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +0200945 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +0200946 *related = 0;
947 return NF_ACCEPT;
948 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200949 /* Fragment header that is before ICMP header tells us that:
950 * it's not an error message since they can't be fragmented.
951 */
David S. Millere7165032012-11-30 12:01:30 -0500952 if (ipvsh->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200953 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +0200954
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200955 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
956 ic->icmp6_type, ntohs(icmpv6_id(ic)),
957 &ipvsh->saddr, &ipvsh->daddr);
Julius Volz2a3b7912008-09-02 15:55:47 +0200958
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200959 /* Now find the contained IP header */
960 ciph.len = ipvsh->len + sizeof(_icmph);
961 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
962 if (ip6h == NULL)
963 return NF_ACCEPT; /* The packet looks wrong, ignore */
964 ciph.saddr.in6 = ip6h->saddr; /* conn_out_get() handles reverse order */
965 ciph.daddr.in6 = ip6h->daddr;
966 /* skip possible IPv6 exthdrs of contained IPv6 packet */
967 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
968 if (ciph.protocol < 0)
969 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
970
971 pp = ip_vs_proto_get(ciph.protocol);
Julius Volz2a3b7912008-09-02 15:55:47 +0200972 if (!pp)
973 return NF_ACCEPT;
974
Julius Volz2a3b7912008-09-02 15:55:47 +0200975 /* The embedded headers contain source and dest in reverse order */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200976 cp = pp->conn_out_get(AF_INET6, skb, &ciph, 1);
Julius Volz2a3b7912008-09-02 15:55:47 +0200977 if (!cp)
978 return NF_ACCEPT;
979
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200980 snet.in6 = ciph.saddr.in6;
981 writable = ciph.len;
982 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
983 pp, writable, sizeof(struct ipv6hdr));
Julius Volz2a3b7912008-09-02 15:55:47 +0200984}
985#endif
986
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100987/*
988 * Check if sctp chunc is ABORT chunk
989 */
990static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
991{
992 sctp_chunkhdr_t *sch, schunk;
993 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
994 sizeof(schunk), &schunk);
995 if (sch == NULL)
996 return 0;
997 if (sch->type == SCTP_CID_ABORT)
998 return 1;
999 return 0;
1000}
1001
Julius Volz2a3b7912008-09-02 15:55:47 +02001002static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 struct tcphdr _tcph, *th;
1005
Julius Volz2a3b7912008-09-02 15:55:47 +02001006 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (th == NULL)
1008 return 0;
1009 return th->rst;
1010}
1011
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001012static inline bool is_new_conn(const struct sk_buff *skb,
1013 struct ip_vs_iphdr *iph)
1014{
1015 switch (iph->protocol) {
1016 case IPPROTO_TCP: {
1017 struct tcphdr _tcph, *th;
1018
1019 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1020 if (th == NULL)
1021 return false;
1022 return th->syn;
1023 }
1024 case IPPROTO_SCTP: {
1025 sctp_chunkhdr_t *sch, schunk;
1026
1027 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
1028 sizeof(schunk), &schunk);
1029 if (sch == NULL)
1030 return false;
1031 return sch->type == SCTP_CID_INIT;
1032 }
1033 default:
1034 return false;
1035 }
1036}
1037
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001038/* Handle response packets: rewrite addresses and send away...
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001039 */
1040static unsigned int
Hans Schillstrom93304192011-01-03 14:44:51 +01001041handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001042 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001043{
Hans Schillstrom93304192011-01-03 14:44:51 +01001044 struct ip_vs_protocol *pp = pd->pp;
1045
Julian Anastasov0d796412010-10-17 16:46:17 +03001046 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001047
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001048 if (!skb_make_writable(skb, iph->len))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001049 goto drop;
1050
1051 /* mangle the packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001052 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001053 goto drop;
1054
1055#ifdef CONFIG_IP_VS_IPV6
1056 if (af == AF_INET6)
1057 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1058 else
1059#endif
1060 {
1061 ip_hdr(skb)->saddr = cp->vaddr.ip;
1062 ip_send_check(ip_hdr(skb));
1063 }
1064
Julian Anastasov8a803042010-09-21 17:38:57 +02001065 /*
1066 * nf_iterate does not expect change in the skb->dst->dev.
1067 * It looks like it is not fatal to enable this code for hooks
1068 * where our handlers are at the end of the chain list and
1069 * when all next handlers use skb->dst->dev and not outdev.
1070 * It will definitely route properly the inout NAT traffic
1071 * when multiple paths are used.
1072 */
1073
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001074 /* For policy routing, packets originating from this
1075 * machine itself may be routed differently to packets
1076 * passing through. We want this packet to be routed as
1077 * if it came from this machine itself. So re-compute
1078 * the routing information.
1079 */
Simon Hormanba4fd7e2011-02-04 18:33:01 +09001080 if (ip_vs_route_me_harder(af, skb))
1081 goto drop;
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001082
Julian Anastasov0d796412010-10-17 16:46:17 +03001083 IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001084
1085 ip_vs_out_stats(cp, skb);
Hans Schillstrom93304192011-01-03 14:44:51 +01001086 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
Julian Anastasovcf356d62010-10-17 16:21:07 +03001087 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001088 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +03001089 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001090 else
1091 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001092 ip_vs_conn_put(cp);
1093
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001094 LeaveFunction(11);
1095 return NF_ACCEPT;
1096
1097drop:
1098 ip_vs_conn_put(cp);
1099 kfree_skb(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001100 LeaveFunction(11);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001101 return NF_STOLEN;
1102}
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104/*
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001105 * Check if outgoing packet belongs to the established ip_vs_conn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 */
1107static unsigned int
Julian Anastasovfc604762010-10-17 16:38:15 +03001108ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Hans Schillstromfc723252011-01-03 14:44:43 +01001110 struct net *net = NULL;
Julius Volz51ef3482008-09-02 15:55:40 +02001111 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001113 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 EnterFunction(11);
1117
Julian Anastasovfc604762010-10-17 16:38:15 +03001118 /* Already marked as IPVS request or reply? */
Harald Welte6869c4d2005-08-09 19:24:19 -07001119 if (skb->ipvs_property)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return NF_ACCEPT;
1121
Julian Anastasovfc604762010-10-17 16:38:15 +03001122 /* Bad... Do not break raw sockets */
1123 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1124 af == AF_INET)) {
1125 struct sock *sk = skb->sk;
1126 struct inet_sock *inet = inet_sk(skb->sk);
1127
1128 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1129 return NF_ACCEPT;
1130 }
1131
1132 if (unlikely(!skb_dst(skb)))
1133 return NF_ACCEPT;
1134
Hans Schillstromfc723252011-01-03 14:44:43 +01001135 net = skb_net(skb);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001136 if (!net_ipvs(net)->enable)
1137 return NF_ACCEPT;
1138
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001139 ip_vs_fill_iph_skb(af, skb, &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001140#ifdef CONFIG_IP_VS_IPV6
1141 if (af == AF_INET6) {
1142 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001143 int related;
1144 int verdict = ip_vs_out_icmp_v6(skb, &related,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001145 hooknum, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Julian Anastasovf5a41842010-10-17 16:35:46 +03001147 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001148 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001149 }
1150 } else
1151#endif
1152 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001153 int related;
1154 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +02001155
Julian Anastasovf5a41842010-10-17 16:35:46 +03001156 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001157 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Hans Schillstrom93304192011-01-03 14:44:51 +01001160 pd = ip_vs_proto_data_get(net, iph.protocol);
1161 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001163 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /* reassemble IP fragments */
Julius Volz2a3b7912008-09-02 15:55:47 +02001166#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001167 if (af == AF_INET)
Julius Volz2a3b7912008-09-02 15:55:47 +02001168#endif
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001169 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001170 if (ip_vs_gather_frags(skb,
1171 ip_vs_defrag_user(hooknum)))
Julius Volz2a3b7912008-09-02 15:55:47 +02001172 return NF_STOLEN;
1173
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001174 ip_vs_fill_ip4hdr(skb_network_header(skb), &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 /*
1178 * Check if the packet belongs to an existing entry
1179 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001180 cp = pp->conn_out_get(af, skb, &iph, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Julian Anastasovcb591552010-10-17 16:40:51 +03001182 if (likely(cp))
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001183 return handle_response(af, skb, pd, cp, &iph);
Simon Horman0cfa5582011-02-04 18:33:01 +09001184 if (sysctl_nat_icmp_send(net) &&
Julian Anastasovcb591552010-10-17 16:40:51 +03001185 (pp->protocol == IPPROTO_TCP ||
1186 pp->protocol == IPPROTO_UDP ||
1187 pp->protocol == IPPROTO_SCTP)) {
1188 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001190 pptr = frag_safe_skb_hp(skb, iph.len,
1191 sizeof(_ports), _ports, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001192 if (pptr == NULL)
1193 return NF_ACCEPT; /* Not for me */
Julian Anastasov276472e2013-03-21 11:58:08 +02001194 if (ip_vs_has_real_service(net, af, iph.protocol, &iph.saddr,
1195 pptr[0])) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001196 /*
1197 * Notify the real server: there is no
1198 * existing entry if it is not RST
1199 * packet or not TCP packet.
1200 */
1201 if ((iph.protocol != IPPROTO_TCP &&
1202 iph.protocol != IPPROTO_SCTP)
1203 || ((iph.protocol == IPPROTO_TCP
1204 && !is_tcp_reset(skb, iph.len))
1205 || (iph.protocol == IPPROTO_SCTP
1206 && !is_sctp_abort(skb,
1207 iph.len)))) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001208#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001209 if (af == AF_INET6) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001210 if (!skb->dev)
1211 skb->dev = net->loopback_dev;
1212 icmpv6_send(skb,
1213 ICMPV6_DEST_UNREACH,
1214 ICMPV6_PORT_UNREACH,
1215 0);
1216 } else
Julius Volz2a3b7912008-09-02 15:55:47 +02001217#endif
Julian Anastasovcb591552010-10-17 16:40:51 +03001218 icmp_send(skb,
1219 ICMP_DEST_UNREACH,
1220 ICMP_PORT_UNREACH, 0);
1221 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Julian Anastasov0d796412010-10-17 16:46:17 +03001225 IP_VS_DBG_PKT(12, af, pp, skb, 0,
Julian Anastasovcb591552010-10-17 16:40:51 +03001226 "ip_vs_out: packet continues traversal as normal");
1227 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Julian Anastasovfc604762010-10-17 16:38:15 +03001230/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001231 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1232 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001233 * Check if packet is reply for established ip_vs_conn.
1234 */
1235static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001236ip_vs_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovfc604762010-10-17 16:38:15 +03001237 const struct net_device *in, const struct net_device *out,
1238 int (*okfn)(struct sk_buff *))
1239{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001240 return ip_vs_out(ops->hooknum, skb, AF_INET);
Julian Anastasovfc604762010-10-17 16:38:15 +03001241}
1242
1243/*
1244 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1245 * Check if packet is reply for established ip_vs_conn.
1246 */
1247static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001248ip_vs_local_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovfc604762010-10-17 16:38:15 +03001249 const struct net_device *in, const struct net_device *out,
1250 int (*okfn)(struct sk_buff *))
1251{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001252 return ip_vs_out(ops->hooknum, skb, AF_INET);
Julian Anastasovfc604762010-10-17 16:38:15 +03001253}
1254
1255#ifdef CONFIG_IP_VS_IPV6
1256
1257/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001258 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1259 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001260 * Check if packet is reply for established ip_vs_conn.
1261 */
1262static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001263ip_vs_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovfc604762010-10-17 16:38:15 +03001264 const struct net_device *in, const struct net_device *out,
1265 int (*okfn)(struct sk_buff *))
1266{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001267 return ip_vs_out(ops->hooknum, skb, AF_INET6);
Julian Anastasovfc604762010-10-17 16:38:15 +03001268}
1269
1270/*
1271 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1272 * Check if packet is reply for established ip_vs_conn.
1273 */
1274static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001275ip_vs_local_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovfc604762010-10-17 16:38:15 +03001276 const struct net_device *in, const struct net_device *out,
1277 int (*okfn)(struct sk_buff *))
1278{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001279 return ip_vs_out(ops->hooknum, skb, AF_INET6);
Julian Anastasovfc604762010-10-17 16:38:15 +03001280}
1281
1282#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284/*
1285 * Handle ICMP messages in the outside-to-inside direction (incoming).
1286 * Find any that might be relevant, check against existing connections,
1287 * forward to the right destination host if relevant.
1288 * Currently handles error types - unreachable, quench, ttl exceeded.
1289 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001290static int
Herbert Xu3db05fe2007-10-15 00:53:15 -07001291ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Hans Schillstrom93304192011-01-03 14:44:51 +01001293 struct net *net = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 struct iphdr *iph;
1295 struct icmphdr _icmph, *ic;
1296 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +02001297 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 struct ip_vs_conn *cp;
1299 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001300 struct ip_vs_proto_data *pd;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001301 unsigned int offset, offset2, ihl, verdict;
1302 bool ipip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 *related = 1;
1305
1306 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001307 if (ip_is_fragment(ip_hdr(skb))) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001308 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001312 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 offset = ihl = iph->ihl * 4;
1314 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1315 if (ic == NULL)
1316 return NF_DROP;
1317
Harvey Harrison14d5e832008-10-31 00:54:29 -07001318 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -07001320 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 /*
1323 * Work through seeing if this is for us.
1324 * These checks are supposed to be in an order that means easy
1325 * things are checked first to speed up processing.... however
1326 * this means that some packets will manage to get a long way
1327 * down this stack and then be rejected, but that's life.
1328 */
1329 if ((ic->type != ICMP_DEST_UNREACH) &&
1330 (ic->type != ICMP_SOURCE_QUENCH) &&
1331 (ic->type != ICMP_TIME_EXCEEDED)) {
1332 *related = 0;
1333 return NF_ACCEPT;
1334 }
1335
1336 /* Now find the contained IP header */
1337 offset += sizeof(_icmph);
1338 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1339 if (cih == NULL)
1340 return NF_ACCEPT; /* The packet looks wrong, ignore */
1341
Hans Schillstrom93304192011-01-03 14:44:51 +01001342 net = skb_net(skb);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001343
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001344 /* Special case for errors for IPIP packets */
1345 ipip = false;
1346 if (cih->protocol == IPPROTO_IPIP) {
1347 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1348 return NF_ACCEPT;
1349 /* Error for our IPIP must arrive at LOCAL_IN */
1350 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1351 return NF_ACCEPT;
1352 offset += cih->ihl * 4;
1353 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1354 if (cih == NULL)
1355 return NF_ACCEPT; /* The packet looks wrong, ignore */
1356 ipip = true;
1357 }
1358
Hans Schillstrom93304192011-01-03 14:44:51 +01001359 pd = ip_vs_proto_data_get(net, cih->protocol);
1360 if (!pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001362 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +09001365 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 pp->dont_defrag))
1367 return NF_ACCEPT;
1368
Julian Anastasov0d796412010-10-17 16:46:17 +03001369 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1370 "Checking incoming ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001372 offset2 = offset;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001373 ip_vs_fill_ip4hdr(cih, &ciph);
1374 ciph.len += offset;
1375 offset = ciph.len;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001376 /* The embedded headers contain source and dest in reverse order.
1377 * For IPIP this is error for request, not for reply.
1378 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001379 cp = pp->conn_in_get(AF_INET, skb, &ciph, ipip ? 0 : 1);
Julian Anastasov6cb90db2011-02-09 02:26:38 +02001380 if (!cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return NF_ACCEPT;
1382
1383 verdict = NF_DROP;
1384
1385 /* Ensure the checksum is correct */
Herbert Xu60476372007-04-09 11:59:39 -07001386 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /* Failed checksum! */
Harvey Harrison14d5e832008-10-31 00:54:29 -07001388 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1389 &iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 goto out;
1391 }
1392
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001393 if (ipip) {
1394 __be32 info = ic->un.gateway;
Peter Christensenf44a5f42014-05-24 21:40:12 +02001395 __u8 type = ic->type;
1396 __u8 code = ic->code;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001397
1398 /* Update the MTU */
1399 if (ic->type == ICMP_DEST_UNREACH &&
1400 ic->code == ICMP_FRAG_NEEDED) {
1401 struct ip_vs_dest *dest = cp->dest;
1402 u32 mtu = ntohs(ic->un.frag.mtu);
Peter Christensenf44a5f42014-05-24 21:40:12 +02001403 __be16 frag_off = cih->frag_off;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001404
1405 /* Strip outer IP and ICMP, go to IPIP header */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001406 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1407 goto ignore_ipip;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001408 offset2 -= ihl + sizeof(_icmph);
1409 skb_reset_network_header(skb);
1410 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1411 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001412 ipv4_update_pmtu(skb, dev_net(skb->dev),
1413 mtu, 0, 0, 0, 0);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001414 /* Client uses PMTUD? */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001415 if (!(frag_off & htons(IP_DF)))
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001416 goto ignore_ipip;
1417 /* Prefer the resulting PMTU */
1418 if (dest) {
Julian Anastasov026ace02013-03-21 11:58:06 +02001419 struct ip_vs_dest_dst *dest_dst;
1420
1421 rcu_read_lock();
1422 dest_dst = rcu_dereference(dest->dest_dst);
1423 if (dest_dst)
1424 mtu = dst_mtu(dest_dst->dst_cache);
1425 rcu_read_unlock();
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001426 }
1427 if (mtu > 68 + sizeof(struct iphdr))
1428 mtu -= sizeof(struct iphdr);
1429 info = htonl(mtu);
1430 }
1431 /* Strip outer IP, ICMP and IPIP, go to IP header of
1432 * original request.
1433 */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001434 if (pskb_pull(skb, offset2) == NULL)
1435 goto ignore_ipip;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001436 skb_reset_network_header(skb);
1437 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1438 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
Peter Christensenf44a5f42014-05-24 21:40:12 +02001439 type, code, ntohl(info));
1440 icmp_send(skb, type, code, info);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001441 /* ICMP can be shorter but anyways, account it */
1442 ip_vs_out_stats(cp, skb);
1443
1444ignore_ipip:
1445 consume_skb(skb);
1446 verdict = NF_STOLEN;
1447 goto out;
1448 }
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 /* do the statistics and put it back */
1451 ip_vs_in_stats(cp, skb);
Julian Anastasov06f3d7f2013-06-18 10:08:06 +03001452 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1453 IPPROTO_SCTP == cih->protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 offset += 2 * sizeof(__u16);
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001455 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Hans Schillstrom552ad652011-06-13 12:19:26 +02001457out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 __ip_vs_conn_put(cp);
1459
1460 return verdict;
1461}
1462
Julius Volz2a3b7912008-09-02 15:55:47 +02001463#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001464static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
1465 unsigned int hooknum, struct ip_vs_iphdr *iph)
Julius Volz2a3b7912008-09-02 15:55:47 +02001466{
Hans Schillstrom93304192011-01-03 14:44:51 +01001467 struct net *net = NULL;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001468 struct ipv6hdr _ip6h, *ip6h;
Julius Volz2a3b7912008-09-02 15:55:47 +02001469 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001470 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +02001471 struct ip_vs_conn *cp;
1472 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001473 struct ip_vs_proto_data *pd;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001474 unsigned int offs_ciph, writable, verdict;
1475
Julius Volz2a3b7912008-09-02 15:55:47 +02001476 *related = 1;
1477
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001478 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001479 if (ic == NULL)
1480 return NF_DROP;
1481
Julius Volz2a3b7912008-09-02 15:55:47 +02001482 /*
1483 * Work through seeing if this is for us.
1484 * These checks are supposed to be in an order that means easy
1485 * things are checked first to speed up processing.... however
1486 * this means that some packets will manage to get a long way
1487 * down this stack and then be rejected, but that's life.
1488 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +02001489 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001490 *related = 0;
1491 return NF_ACCEPT;
1492 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001493 /* Fragment header that is before ICMP header tells us that:
1494 * it's not an error message since they can't be fragmented.
1495 */
David S. Millere7165032012-11-30 12:01:30 -05001496 if (iph->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001497 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +02001498
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001499 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1500 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1501 &iph->saddr, &iph->daddr);
1502
Julius Volz2a3b7912008-09-02 15:55:47 +02001503 /* Now find the contained IP header */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001504 ciph.len = iph->len + sizeof(_icmph);
1505 offs_ciph = ciph.len; /* Save ip header offset */
1506 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
1507 if (ip6h == NULL)
Julius Volz2a3b7912008-09-02 15:55:47 +02001508 return NF_ACCEPT; /* The packet looks wrong, ignore */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001509 ciph.saddr.in6 = ip6h->saddr; /* conn_in_get() handles reverse order */
1510 ciph.daddr.in6 = ip6h->daddr;
1511 /* skip possible IPv6 exthdrs of contained IPv6 packet */
1512 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
1513 if (ciph.protocol < 0)
1514 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
Julius Volz2a3b7912008-09-02 15:55:47 +02001515
Hans Schillstrom93304192011-01-03 14:44:51 +01001516 net = skb_net(skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001517 pd = ip_vs_proto_data_get(net, ciph.protocol);
Hans Schillstrom93304192011-01-03 14:44:51 +01001518 if (!pd)
Julius Volz2a3b7912008-09-02 15:55:47 +02001519 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001520 pp = pd->pp;
Julius Volz2a3b7912008-09-02 15:55:47 +02001521
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001522 /* Cannot handle fragmented embedded protocol */
1523 if (ciph.fragoffs)
Julius Volz2a3b7912008-09-02 15:55:47 +02001524 return NF_ACCEPT;
1525
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001526 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offs_ciph,
Julian Anastasov0d796412010-10-17 16:46:17 +03001527 "Checking incoming ICMPv6 for");
Julius Volz2a3b7912008-09-02 15:55:47 +02001528
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001529 /* The embedded headers contain source and dest in reverse order
1530 * if not from localhost
1531 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001532 cp = pp->conn_in_get(AF_INET6, skb, &ciph,
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001533 (hooknum == NF_INET_LOCAL_OUT) ? 0 : 1);
1534
Julian Anastasov6cb90db2011-02-09 02:26:38 +02001535 if (!cp)
Julius Volz2a3b7912008-09-02 15:55:47 +02001536 return NF_ACCEPT;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001537 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1538 if ((hooknum == NF_INET_LOCAL_OUT) &&
1539 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
1540 __ip_vs_conn_put(cp);
1541 return NF_ACCEPT;
1542 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001543
Julius Volz2a3b7912008-09-02 15:55:47 +02001544 /* do the statistics and put it back */
1545 ip_vs_in_stats(cp, skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001546
1547 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1548 writable = ciph.len;
1549 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1550 IPPROTO_SCTP == ciph.protocol)
1551 writable += 2 * sizeof(__u16); /* Also mangle ports */
1552
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001553 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, writable, hooknum, &ciph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001554
1555 __ip_vs_conn_put(cp);
1556
1557 return verdict;
1558}
1559#endif
1560
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562/*
1563 * Check if it's for virtual services, look it up,
1564 * and send it on its way...
1565 */
1566static unsigned int
Julian Anastasovcb591552010-10-17 16:40:51 +03001567ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Hans Schillstromf1313152011-01-03 14:44:55 +01001569 struct net *net;
Julius Volz51ef3482008-09-02 15:55:40 +02001570 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001572 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 struct ip_vs_conn *cp;
Simon Horman4a516f12011-09-16 14:11:49 +09001574 int ret, pkts;
Hans Schillstromf1313152011-01-03 14:44:55 +01001575 struct netns_ipvs *ipvs;
Julius Volz51ef3482008-09-02 15:55:40 +02001576
Julian Anastasovfc604762010-10-17 16:38:15 +03001577 /* Already marked as IPVS request or reply? */
1578 if (skb->ipvs_property)
1579 return NF_ACCEPT;
1580
Julian Anastasovcb591552010-10-17 16:40:51 +03001581 /*
1582 * Big tappo:
1583 * - remote client: only PACKET_HOST
1584 * - route: used for struct net when skb->dev is unset
1585 */
1586 if (unlikely((skb->pkt_type != PACKET_HOST &&
1587 hooknum != NF_INET_LOCAL_OUT) ||
1588 !skb_dst(skb))) {
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001589 ip_vs_fill_iph_skb(af, skb, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001590 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1591 " ignored in hook %u\n",
1592 skb->pkt_type, iph.protocol,
1593 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1594 return NF_ACCEPT;
1595 }
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001596 /* ipvs enabled in this netns ? */
1597 net = skb_net(skb);
Julian Anastasov0c125822013-03-09 23:25:04 +02001598 ipvs = net_ipvs(net);
1599 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001600 return NF_ACCEPT;
1601
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001602 ip_vs_fill_iph_skb(af, skb, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Julian Anastasovcb591552010-10-17 16:40:51 +03001604 /* Bad... Do not break raw sockets */
1605 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1606 af == AF_INET)) {
1607 struct sock *sk = skb->sk;
1608 struct inet_sock *inet = inet_sk(skb->sk);
1609
1610 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1611 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613
Julius Volz94b26552009-08-31 16:22:23 +02001614#ifdef CONFIG_IP_VS_IPV6
1615 if (af == AF_INET6) {
1616 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001617 int related;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001618 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum,
1619 &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Julius Volz94b26552009-08-31 16:22:23 +02001621 if (related)
1622 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001623 }
1624 } else
1625#endif
1626 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001627 int related;
1628 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
Julius Volz94b26552009-08-31 16:22:23 +02001629
1630 if (related)
1631 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 /* Protocol supported? */
Hans Schillstrom93304192011-01-03 14:44:51 +01001635 pd = ip_vs_proto_data_get(net, iph.protocol);
1636 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001638 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /*
1640 * Check if the packet belongs to an existing connection entry
1641 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001642 cp = pp->conn_in_get(af, skb, &iph, 0);
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001643
1644 if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp && cp->dest &&
1645 unlikely(!atomic_read(&cp->dest->weight)) && !iph.fragoffs &&
1646 is_new_conn(skb, &iph)) {
1647 ip_vs_conn_expire_now(cp);
1648 __ip_vs_conn_put(cp);
1649 cp = NULL;
1650 }
1651
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001652 if (unlikely(!cp) && !iph.fragoffs) {
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001653 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1654 * replayed fragment zero will already have created the cp
1655 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 int v;
1657
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001658 /* Schedule and create new connection entry into &cp */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001659 if (!pp->conn_schedule(af, skb, pd, &v, &cp, &iph))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return v;
1661 }
1662
1663 if (unlikely(!cp)) {
1664 /* sorry, all this trouble for a no-hit :) */
Julian Anastasov0d796412010-10-17 16:46:17 +03001665 IP_VS_DBG_PKT(12, af, pp, skb, 0,
Julian Anastasovcb591552010-10-17 16:40:51 +03001666 "ip_vs_in: packet continues traversal as normal");
Jiri Pirko6aafeef2013-11-06 17:52:20 +01001667 if (iph.fragoffs) {
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001668 /* Fragment that couldn't be mapped to a conn entry
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001669 * is missing module nf_defrag_ipv6
1670 */
1671 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1672 IP_VS_DBG_PKT(7, af, pp, skb, 0, "unhandled fragment");
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return NF_ACCEPT;
1675 }
1676
Julian Anastasov0d796412010-10-17 16:46:17 +03001677 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /* Check the server status */
1679 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1680 /* the destination server is not available */
1681
Simon Horman71a8ab62011-02-04 18:33:01 +09001682 if (sysctl_expire_nodest_conn(ipvs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /* try to expire the connection immediately */
1684 ip_vs_conn_expire_now(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
Julian Anastasovdc8103f2005-11-08 09:40:05 -08001686 /* don't restart its timer, and silently
1687 drop the packet. */
1688 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return NF_DROP;
1690 }
1691
1692 ip_vs_in_stats(cp, skb);
Simon Horman4a516f12011-09-16 14:11:49 +09001693 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (cp->packet_xmit)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001695 ret = cp->packet_xmit(skb, cp, pp, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 /* do not touch skb anymore */
1697 else {
1698 IP_VS_DBG_RL("warning: packet_xmit is null");
1699 ret = NF_ACCEPT;
1700 }
1701
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001702 /* Increase its packet counter and check if it is needed
1703 * to be synchronized
1704 *
1705 * Sync connection if it is about to close to
1706 * encorage the standby servers to update the connections timeout
Hans Schillstrom986a0752010-11-19 14:25:13 +01001707 *
1708 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001709 */
Hans Schillstromf1313152011-01-03 14:44:55 +01001710
Hans Schillstrom986a0752010-11-19 14:25:13 +01001711 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
Simon Horman59e03502011-02-04 18:33:01 +09001712 pkts = sysctl_sync_threshold(ipvs);
Hans Schillstrom986a0752010-11-19 14:25:13 +01001713 else
1714 pkts = atomic_add_return(1, &cp->in_pkts);
1715
Julian Anastasov749c42b2012-04-24 23:46:40 +03001716 if (ipvs->sync_state & IP_VS_STATE_MASTER)
1717 ip_vs_sync_conn(net, cp, pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 ip_vs_conn_put(cp);
1720 return ret;
1721}
1722
Julian Anastasovcb591552010-10-17 16:40:51 +03001723/*
1724 * AF_INET handler in NF_INET_LOCAL_IN chain
1725 * Schedule and forward packets from remote clients
1726 */
1727static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001728ip_vs_remote_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovcb591552010-10-17 16:40:51 +03001729 const struct net_device *in,
1730 const struct net_device *out,
1731 int (*okfn)(struct sk_buff *))
1732{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001733 return ip_vs_in(ops->hooknum, skb, AF_INET);
Julian Anastasovcb591552010-10-17 16:40:51 +03001734}
1735
1736/*
1737 * AF_INET handler in NF_INET_LOCAL_OUT chain
1738 * Schedule and forward packets from local clients
1739 */
1740static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001741ip_vs_local_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovcb591552010-10-17 16:40:51 +03001742 const struct net_device *in, const struct net_device *out,
1743 int (*okfn)(struct sk_buff *))
1744{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001745 return ip_vs_in(ops->hooknum, skb, AF_INET);
Julian Anastasovcb591552010-10-17 16:40:51 +03001746}
1747
1748#ifdef CONFIG_IP_VS_IPV6
1749
1750/*
1751 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1752 * Schedule and forward packets from remote clients
1753 */
1754static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001755ip_vs_remote_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovcb591552010-10-17 16:40:51 +03001756 const struct net_device *in,
1757 const struct net_device *out,
1758 int (*okfn)(struct sk_buff *))
1759{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001760 return ip_vs_in(ops->hooknum, skb, AF_INET6);
Julian Anastasovcb591552010-10-17 16:40:51 +03001761}
1762
1763/*
1764 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1765 * Schedule and forward packets from local clients
1766 */
1767static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001768ip_vs_local_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovcb591552010-10-17 16:40:51 +03001769 const struct net_device *in, const struct net_device *out,
1770 int (*okfn)(struct sk_buff *))
1771{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001772 return ip_vs_in(ops->hooknum, skb, AF_INET6);
Julian Anastasovcb591552010-10-17 16:40:51 +03001773}
1774
1775#endif
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778/*
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001779 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * related packets destined for 0.0.0.0/0.
1781 * When fwmark-based virtual service is used, such as transparent
1782 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1783 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001784 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 * and send them to ip_vs_in_icmp.
1786 */
1787static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001788ip_vs_forward_icmp(const struct nf_hook_ops *ops, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 const struct net_device *in, const struct net_device *out,
1790 int (*okfn)(struct sk_buff *))
1791{
1792 int r;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001793 struct net *net;
Julian Anastasov0c125822013-03-09 23:25:04 +02001794 struct netns_ipvs *ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Herbert Xu3db05fe2007-10-15 00:53:15 -07001796 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return NF_ACCEPT;
1798
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001799 /* ipvs enabled in this netns ? */
1800 net = skb_net(skb);
Julian Anastasov0c125822013-03-09 23:25:04 +02001801 ipvs = net_ipvs(net);
1802 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001803 return NF_ACCEPT;
1804
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001805 return ip_vs_in_icmp(skb, &r, ops->hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
Julius Volz2a3b7912008-09-02 15:55:47 +02001808#ifdef CONFIG_IP_VS_IPV6
1809static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001810ip_vs_forward_icmp_v6(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julius Volz2a3b7912008-09-02 15:55:47 +02001811 const struct net_device *in, const struct net_device *out,
1812 int (*okfn)(struct sk_buff *))
1813{
1814 int r;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001815 struct net *net;
Julian Anastasov0c125822013-03-09 23:25:04 +02001816 struct netns_ipvs *ipvs;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001817 struct ip_vs_iphdr iphdr;
Julius Volz2a3b7912008-09-02 15:55:47 +02001818
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001819 ip_vs_fill_iph_skb(AF_INET6, skb, &iphdr);
1820 if (iphdr.protocol != IPPROTO_ICMPV6)
Julius Volz2a3b7912008-09-02 15:55:47 +02001821 return NF_ACCEPT;
1822
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001823 /* ipvs enabled in this netns ? */
1824 net = skb_net(skb);
Julian Anastasov0c125822013-03-09 23:25:04 +02001825 ipvs = net_ipvs(net);
1826 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001827 return NF_ACCEPT;
1828
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001829 return ip_vs_in_icmp_v6(skb, &r, ops->hooknum, &iphdr);
Julius Volz2a3b7912008-09-02 15:55:47 +02001830}
1831#endif
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Patrick McHardy19994142007-12-05 01:23:00 -08001834static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
Julian Anastasovcb591552010-10-17 16:40:51 +03001835 /* After packet filtering, change source only for VS/NAT */
1836 {
1837 .hook = ip_vs_reply4,
1838 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001839 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001840 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001841 .priority = NF_IP_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001842 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001843 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1844 * or VS/NAT(change destination), so that filtering rules can be
1845 * applied to IPVS. */
1846 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001847 .hook = ip_vs_remote_request4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001848 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001849 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001850 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001851 .priority = NF_IP_PRI_NAT_SRC - 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001852 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001853 /* Before ip_vs_in, change source only for VS/NAT */
Patrick McHardy41c5b312007-12-05 01:22:43 -08001854 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001855 .hook = ip_vs_local_reply4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001856 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001857 .pf = NFPROTO_IPV4,
Julian Anastasovfc604762010-10-17 16:38:15 +03001858 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001859 .priority = NF_IP_PRI_NAT_DST + 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001860 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001861 /* After mangle, schedule and forward local requests */
1862 {
1863 .hook = ip_vs_local_request4,
1864 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001865 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001866 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001867 .priority = NF_IP_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001868 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001869 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1870 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1871 {
1872 .hook = ip_vs_forward_icmp,
1873 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001874 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001875 .hooknum = NF_INET_FORWARD,
1876 .priority = 99,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001877 },
Julian Anastasovfc604762010-10-17 16:38:15 +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 Anastasovfc604762010-10-17 16:38:15 +03001883 .hooknum = NF_INET_FORWARD,
1884 .priority = 100,
1885 },
Julius Volz473b23d2008-09-02 15:55:54 +02001886#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001887 /* After packet filtering, change source only for VS/NAT */
1888 {
1889 .hook = ip_vs_reply6,
1890 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001891 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001892 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001893 .priority = NF_IP6_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001894 },
Julius Volz473b23d2008-09-02 15:55:54 +02001895 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1896 * or VS/NAT(change destination), so that filtering rules can be
1897 * applied to IPVS. */
1898 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001899 .hook = ip_vs_remote_request6,
Julius Volz473b23d2008-09-02 15:55:54 +02001900 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001901 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001902 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001903 .priority = NF_IP6_PRI_NAT_SRC - 1,
Julius Volz473b23d2008-09-02 15:55:54 +02001904 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001905 /* Before ip_vs_in, change source only for VS/NAT */
Julius Volz473b23d2008-09-02 15:55:54 +02001906 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001907 .hook = ip_vs_local_reply6,
Julius Volz473b23d2008-09-02 15:55:54 +02001908 .owner = THIS_MODULE,
Julian Anastasoveb90b0c2014-08-22 17:53:41 +03001909 .pf = NFPROTO_IPV6,
Julian Anastasovfc604762010-10-17 16:38:15 +03001910 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001911 .priority = NF_IP6_PRI_NAT_DST + 1,
Julius Volz473b23d2008-09-02 15:55:54 +02001912 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001913 /* After mangle, schedule and forward local requests */
1914 {
1915 .hook = ip_vs_local_request6,
1916 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001917 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001918 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001919 .priority = NF_IP6_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001920 },
Julius Volz473b23d2008-09-02 15:55:54 +02001921 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1922 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1923 {
1924 .hook = ip_vs_forward_icmp_v6,
1925 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001926 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001927 .hooknum = NF_INET_FORWARD,
1928 .priority = 99,
Julius Volz473b23d2008-09-02 15:55:54 +02001929 },
Julian Anastasovfc604762010-10-17 16:38:15 +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 Anastasovfc604762010-10-17 16:38:15 +03001935 .hooknum = NF_INET_FORWARD,
1936 .priority = 100,
1937 },
Julius Volz473b23d2008-09-02 15:55:54 +02001938#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939};
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001940/*
1941 * Initialize IP Virtual Server netns mem.
1942 */
1943static int __net_init __ip_vs_init(struct net *net)
1944{
1945 struct netns_ipvs *ipvs;
1946
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001947 ipvs = net_generic(net, ip_vs_net_id);
Joe Perches0a9ee812011-08-29 14:17:25 -07001948 if (ipvs == NULL)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001949 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -07001950
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001951 /* Hold the beast until a service is registerd */
1952 ipvs->enable = 0;
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001953 ipvs->net = net;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001954 /* Counters used for creating unique names */
1955 ipvs->gen = atomic_read(&ipvs_netns_cnt);
1956 atomic_inc(&ipvs_netns_cnt);
1957 net->ipvs = ipvs;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001958
Hans Schillstrom503cf152011-05-01 18:50:16 +02001959 if (ip_vs_estimator_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001960 goto estimator_fail;
1961
Hans Schillstrom503cf152011-05-01 18:50:16 +02001962 if (ip_vs_control_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001963 goto control_fail;
1964
Hans Schillstrom503cf152011-05-01 18:50:16 +02001965 if (ip_vs_protocol_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001966 goto protocol_fail;
1967
Hans Schillstrom503cf152011-05-01 18:50:16 +02001968 if (ip_vs_app_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001969 goto app_fail;
1970
Hans Schillstrom503cf152011-05-01 18:50:16 +02001971 if (ip_vs_conn_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001972 goto conn_fail;
1973
Hans Schillstrom503cf152011-05-01 18:50:16 +02001974 if (ip_vs_sync_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001975 goto sync_fail;
1976
Simon Hormana870c8c2011-02-01 18:21:53 +01001977 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001978 sizeof(struct netns_ipvs), ipvs->gen);
1979 return 0;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001980/*
1981 * Error handling
1982 */
1983
1984sync_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02001985 ip_vs_conn_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001986conn_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02001987 ip_vs_app_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001988app_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02001989 ip_vs_protocol_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001990protocol_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02001991 ip_vs_control_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001992control_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02001993 ip_vs_estimator_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001994estimator_fail:
Julian Anastasov39f618b2012-04-25 00:29:58 +03001995 net->ipvs = NULL;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001996 return -ENOMEM;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001997}
1998
1999static void __net_exit __ip_vs_cleanup(struct net *net)
2000{
Hans Schillstrom503cf152011-05-01 18:50:16 +02002001 ip_vs_service_net_cleanup(net); /* ip_vs_flush() with locks */
2002 ip_vs_conn_net_cleanup(net);
2003 ip_vs_app_net_cleanup(net);
2004 ip_vs_protocol_net_cleanup(net);
2005 ip_vs_control_net_cleanup(net);
2006 ip_vs_estimator_net_cleanup(net);
Hans Schillstrom1ae132b2011-05-03 22:09:30 +02002007 IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net)->gen);
Julian Anastasov39f618b2012-04-25 00:29:58 +03002008 net->ipvs = NULL;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002009}
2010
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002011static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2012{
2013 EnterFunction(2);
2014 net_ipvs(net)->enable = 0; /* Disable packet reception */
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +02002015 smp_wmb();
Hans Schillstrom503cf152011-05-01 18:50:16 +02002016 ip_vs_sync_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002017 LeaveFunction(2);
2018}
2019
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002020static struct pernet_operations ipvs_core_ops = {
2021 .init = __ip_vs_init,
2022 .exit = __ip_vs_cleanup,
2023 .id = &ip_vs_net_id,
2024 .size = sizeof(struct netns_ipvs),
2025};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002027static struct pernet_operations ipvs_core_dev_ops = {
2028 .exit = __ip_vs_dev_cleanup,
2029};
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031/*
2032 * Initialize IP Virtual Server
2033 */
2034static int __init ip_vs_init(void)
2035{
2036 int ret;
2037
2038 ret = ip_vs_control_init();
2039 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002040 pr_err("can't setup control.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002041 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043
2044 ip_vs_protocol_init();
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 ret = ip_vs_conn_init();
2047 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002048 pr_err("can't setup connection table.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002049 goto cleanup_protocol;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002050 }
2051
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002052 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2053 if (ret < 0)
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002054 goto cleanup_conn;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002055
2056 ret = register_pernet_device(&ipvs_core_dev_ops);
2057 if (ret < 0)
2058 goto cleanup_sub;
2059
Patrick McHardy41c5b312007-12-05 01:22:43 -08002060 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002062 pr_err("can't register hooks.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002063 goto cleanup_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065
Hans Schillstrom8537de82012-04-26 07:47:44 +02002066 ret = ip_vs_register_nl_ioctl();
2067 if (ret < 0) {
2068 pr_err("can't register netlink/ioctl.\n");
2069 goto cleanup_hooks;
2070 }
2071
Hannes Eder1e3e2382009-08-02 11:05:41 +00002072 pr_info("ipvs loaded.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return ret;
2075
Hans Schillstrom8537de82012-04-26 07:47:44 +02002076cleanup_hooks:
2077 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002078cleanup_dev:
2079 unregister_pernet_device(&ipvs_core_dev_ops);
2080cleanup_sub:
2081 unregister_pernet_subsys(&ipvs_core_ops);
Hans Schillstrom552ad652011-06-13 12:19:26 +02002082cleanup_conn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 ip_vs_conn_cleanup();
Hans Schillstrom552ad652011-06-13 12:19:26 +02002084cleanup_protocol:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 ip_vs_protocol_cleanup();
2086 ip_vs_control_cleanup();
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002087exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return ret;
2089}
2090
2091static void __exit ip_vs_cleanup(void)
2092{
Hans Schillstrom8537de82012-04-26 07:47:44 +02002093 ip_vs_unregister_nl_ioctl();
Patrick McHardy41c5b312007-12-05 01:22:43 -08002094 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002095 unregister_pernet_device(&ipvs_core_dev_ops);
2096 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 ip_vs_conn_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 ip_vs_protocol_cleanup();
2099 ip_vs_control_cleanup();
Hannes Eder1e3e2382009-08-02 11:05:41 +00002100 pr_info("ipvs unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
2103module_init(ip_vs_init);
2104module_exit(ip_vs_cleanup);
2105MODULE_LICENSE("GPL");