blob: 4f69e83ff836b0ec415342772055a58a52c25fc4 [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:
100 sprintf(buf, "IP_%d", proto);
101 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Hans Schillstromb17fc992011-01-03 14:44:56 +0100120 s = this_cpu_ptr(dest->stats.cpustats);
121 s->ustats.inpkts++;
122 u64_stats_update_begin(&s->syncp);
123 s->ustats.inbytes += skb->len;
124 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Hans Schillstromb17fc992011-01-03 14:44:56 +0100126 s = this_cpu_ptr(dest->svc->stats.cpustats);
127 s->ustats.inpkts++;
128 u64_stats_update_begin(&s->syncp);
129 s->ustats.inbytes += skb->len;
130 u64_stats_update_end(&s->syncp);
131
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200132 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100133 s->ustats.inpkts++;
134 u64_stats_update_begin(&s->syncp);
135 s->ustats.inbytes += skb->len;
136 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138}
139
140
141static inline void
142ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
143{
144 struct ip_vs_dest *dest = cp->dest;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100145 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
Hans Schillstromb17fc992011-01-03 14:44:56 +0100148 struct ip_vs_cpu_stats *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Hans Schillstromb17fc992011-01-03 14:44:56 +0100150 s = this_cpu_ptr(dest->stats.cpustats);
151 s->ustats.outpkts++;
152 u64_stats_update_begin(&s->syncp);
153 s->ustats.outbytes += skb->len;
154 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Hans Schillstromb17fc992011-01-03 14:44:56 +0100156 s = this_cpu_ptr(dest->svc->stats.cpustats);
157 s->ustats.outpkts++;
158 u64_stats_update_begin(&s->syncp);
159 s->ustats.outbytes += skb->len;
160 u64_stats_update_end(&s->syncp);
161
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200162 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100163 s->ustats.outpkts++;
164 u64_stats_update_begin(&s->syncp);
165 s->ustats.outbytes += skb->len;
166 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168}
169
170
171static inline void
172ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
173{
Hans Schillstromb17fc992011-01-03 14:44:56 +0100174 struct netns_ipvs *ipvs = net_ipvs(svc->net);
175 struct ip_vs_cpu_stats *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Hans Schillstromb17fc992011-01-03 14:44:56 +0100177 s = this_cpu_ptr(cp->dest->stats.cpustats);
178 s->ustats.conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Hans Schillstromb17fc992011-01-03 14:44:56 +0100180 s = this_cpu_ptr(svc->stats.cpustats);
181 s->ustats.conns++;
182
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200183 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100184 s->ustats.conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187
Simon Horman4a516f12011-09-16 14:11:49 +0900188static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189ip_vs_set_state(struct ip_vs_conn *cp, int direction,
190 const struct sk_buff *skb,
Hans Schillstrom93304192011-01-03 14:44:51 +0100191 struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Simon Horman4a516f12011-09-16 14:11:49 +0900193 if (likely(pd->pp->state_transition))
194 pd->pp->state_transition(cp, direction, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Hans Schillstroma5959d52010-11-19 14:25:10 +0100197static inline int
Simon Horman85999282010-08-22 21:37:53 +0900198ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
199 struct sk_buff *skb, int protocol,
200 const union nf_inet_addr *caddr, __be16 cport,
201 const union nf_inet_addr *vaddr, __be16 vport,
202 struct ip_vs_conn_param *p)
203{
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100204 ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr,
205 vport, p);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200206 p->pe = rcu_dereference(svc->pe);
Simon Horman85999282010-08-22 21:37:53 +0900207 if (p->pe && p->pe->fill_param)
Hans Schillstroma5959d52010-11-19 14:25:10 +0100208 return p->pe->fill_param(p, skb);
209
210 return 0;
Simon Horman85999282010-08-22 21:37:53 +0900211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/*
214 * IPVS persistent scheduling function
215 * It creates a connection entry according to its template if exists,
216 * or selects a server and creates a connection entry plus a template.
217 * Locking: we are svc user (svc->refcnt), so we hold all dests too
218 * Protocols supported: TCP, UDP
219 */
220static struct ip_vs_conn *
221ip_vs_sched_persist(struct ip_vs_service *svc,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200222 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
223 int *ignored, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct ip_vs_conn *cp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 struct ip_vs_dest *dest;
227 struct ip_vs_conn *ct;
Simon Horman5b57a982010-08-22 21:37:51 +0900228 __be16 dport = 0; /* destination port to forward */
Julian Anastasov35757922010-09-17 14:18:16 +0200229 unsigned int flags;
Simon Hormanf11017e2010-08-22 21:37:52 +0900230 struct ip_vs_conn_param param;
Simon Hormane0aac522012-01-27 10:45:27 +0900231 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
Julius Volz28364a52008-09-02 15:55:43 +0200232 union nf_inet_addr snet; /* source network of the client,
233 after masking */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* Mask saddr with the netmask to adjust template granularity */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200236#ifdef CONFIG_IP_VS_IPV6
237 if (svc->af == AF_INET6)
Julian Anastasov0a925862013-04-17 23:50:49 +0300238 ipv6_addr_prefix(&snet.in6, &iph->saddr.in6,
239 (__force __u32) svc->netmask);
Julius Volzcd17f9e2008-09-02 15:55:46 +0200240 else
241#endif
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200242 snet.ip = iph->saddr.ip & svc->netmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Julius Volzcd17f9e2008-09-02 15:55:46 +0200244 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
245 "mnet %s\n",
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200246 IP_VS_DBG_ADDR(svc->af, &iph->saddr), ntohs(src_port),
247 IP_VS_DBG_ADDR(svc->af, &iph->daddr), ntohs(dst_port),
Julius Volzcd17f9e2008-09-02 15:55:46 +0200248 IP_VS_DBG_ADDR(svc->af, &snet));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /*
251 * As far as we know, FTP is a very complicated network protocol, and
252 * it uses control connection and data connections. For active FTP,
253 * FTP server initialize data connection to the client, its source port
254 * is often 20. For passive FTP, FTP server tells the clients the port
255 * that it passively listens to, and the client issues the data
256 * connection. In the tunneling or direct routing mode, the load
257 * balancer is on the client-to-server half of connection, the port
258 * number is unknown to the load balancer. So, a conn template like
259 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
260 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
261 * is created for other persistent services.
262 */
Simon Horman5b57a982010-08-22 21:37:51 +0900263 {
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200264 int protocol = iph->protocol;
265 const union nf_inet_addr *vaddr = &iph->daddr;
Simon Hormanf11017e2010-08-22 21:37:52 +0900266 __be16 vport = 0;
267
Hans Schillstromce144f22010-11-19 14:25:08 +0100268 if (dst_port == svc->port) {
Simon Horman5b57a982010-08-22 21:37:51 +0900269 /* non-FTP template:
270 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
271 * FTP template:
272 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
274 if (svc->port != FTPPORT)
Hans Schillstromce144f22010-11-19 14:25:08 +0100275 vport = dst_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900277 /* Note: persistent fwmark-based services and
278 * persistent port zero service are handled here.
279 * fwmark template:
280 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
281 * port zero template:
282 * <protocol,caddr,0,vaddr,0,daddr,0>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
Julius Volz28364a52008-09-02 15:55:43 +0200284 if (svc->fwmark) {
Simon Horman5b57a982010-08-22 21:37:51 +0900285 protocol = IPPROTO_IP;
286 vaddr = &fwmark;
287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Hans Schillstroma5959d52010-11-19 14:25:10 +0100289 /* return *ignored = -1 so NF_DROP can be used */
290 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
291 vaddr, vport, &param) < 0) {
292 *ignored = -1;
293 return NULL;
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
Simon Horman5b57a982010-08-22 21:37:51 +0900297 /* Check if a template already exists */
Simon Hormanf11017e2010-08-22 21:37:52 +0900298 ct = ip_vs_ct_in_get(&param);
Simon Horman5b57a982010-08-22 21:37:51 +0900299 if (!ct || !ip_vs_check_template(ct)) {
Julian Anastasovceec4c32013-03-22 11:46:53 +0200300 struct ip_vs_scheduler *sched;
301
Hans Schillstroma5959d52010-11-19 14:25:10 +0100302 /*
303 * No template found or the dest of the connection
Simon Horman5b57a982010-08-22 21:37:51 +0900304 * template is not available.
Hans Schillstroma5959d52010-11-19 14:25:10 +0100305 * return *ignored=0 i.e. ICMP and NF_DROP
Simon Horman5b57a982010-08-22 21:37:51 +0900306 */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200307 sched = rcu_dereference(svc->scheduler);
Julian Anastasovbba54de2013-06-16 09:09:36 +0300308 dest = sched->schedule(svc, skb, iph);
Simon Horman5b57a982010-08-22 21:37:51 +0900309 if (!dest) {
310 IP_VS_DBG(1, "p-schedule: no dest found.\n");
Simon Horman85999282010-08-22 21:37:53 +0900311 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100312 *ignored = 0;
Simon Horman5b57a982010-08-22 21:37:51 +0900313 return NULL;
314 }
315
Hans Schillstromce144f22010-11-19 14:25:08 +0100316 if (dst_port == svc->port && svc->port != FTPPORT)
Simon Horman5b57a982010-08-22 21:37:51 +0900317 dport = dest->port;
318
Simon Horman85999282010-08-22 21:37:53 +0900319 /* Create a template
320 * This adds param.pe_data to the template,
321 * and thus param.pe_data will be destroyed
322 * when the template expires */
Simon Hormanf11017e2010-08-22 21:37:52 +0900323 ct = ip_vs_conn_new(&param, &dest->addr, dport,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100324 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
Simon Horman85999282010-08-22 21:37:53 +0900325 if (ct == NULL) {
326 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100327 *ignored = -1;
Simon Horman5b57a982010-08-22 21:37:51 +0900328 return NULL;
Simon Horman85999282010-08-22 21:37:53 +0900329 }
Simon Horman5b57a982010-08-22 21:37:51 +0900330
331 ct->timeout = svc->timeout;
Simon Horman85999282010-08-22 21:37:53 +0900332 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900333 /* set destination with the found template */
334 dest = ct->dest;
Simon Horman85999282010-08-22 21:37:53 +0900335 kfree(param.pe_data);
336 }
Simon Horman5b57a982010-08-22 21:37:51 +0900337
Hans Schillstromce144f22010-11-19 14:25:08 +0100338 dport = dst_port;
Simon Horman5b57a982010-08-22 21:37:51 +0900339 if (dport == svc->port && dest->port)
340 dport = dest->port;
341
Nick Chalk26ec0372010-06-22 08:07:01 +0200342 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200343 && iph->protocol == IPPROTO_UDP) ?
Nick Chalk26ec0372010-06-22 08:07:01 +0200344 IP_VS_CONN_F_ONE_PACKET : 0;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /*
347 * Create a new connection according to the template
348 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200349 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol, &iph->saddr,
350 src_port, &iph->daddr, dst_port, &param);
Hans Schillstromce144f22010-11-19 14:25:08 +0100351
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100352 cp = ip_vs_conn_new(&param, &dest->addr, dport, flags, dest, skb->mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (cp == NULL) {
354 ip_vs_conn_put(ct);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100355 *ignored = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return NULL;
357 }
358
359 /*
360 * Add its control
361 */
362 ip_vs_control_add(cp, ct);
363 ip_vs_conn_put(ct);
364
365 ip_vs_conn_stats(cp, svc);
366 return cp;
367}
368
369
370/*
371 * IPVS main scheduling function
372 * It selects a server according to the virtual service, and
373 * creates a connection entry.
374 * Protocols supported: TCP, UDP
Hans Schillstroma5959d52010-11-19 14:25:10 +0100375 *
376 * Usage of *ignored
377 *
378 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
379 * svc/scheduler decides that this packet should be accepted with
380 * NF_ACCEPT because it must not be scheduled.
381 *
382 * 0 : scheduler can not find destination, so try bypass or
383 * return ICMP and then NF_DROP (ip_vs_leave).
384 *
385 * -1 : scheduler tried to schedule but fatal error occurred, eg.
386 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
387 * failure such as missing Call-ID, ENOMEM on skb_linearize
388 * or pe_data. In this case we should return NF_DROP without
389 * any attempts to send ICMP with ip_vs_leave.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
391struct ip_vs_conn *
Julian Anastasov190ecd22010-10-17 16:24:37 +0300392ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200393 struct ip_vs_proto_data *pd, int *ignored,
394 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Hans Schillstrom93304192011-01-03 14:44:51 +0100396 struct ip_vs_protocol *pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct ip_vs_conn *cp = NULL;
Julian Anastasovceec4c32013-03-22 11:46:53 +0200398 struct ip_vs_scheduler *sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct ip_vs_dest *dest;
Julian Anastasov35757922010-09-17 14:18:16 +0200400 __be16 _ports[2], *pptr;
401 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Julian Anastasov190ecd22010-10-17 16:24:37 +0300403 *ignored = 1;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200404 /*
405 * IPv6 frags, only the first hit here.
406 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200407 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (pptr == NULL)
409 return NULL;
410
411 /*
Julian Anastasov190ecd22010-10-17 16:24:37 +0300412 * FTPDATA needs this check when using local real server.
413 * Never schedule Active FTPDATA connections from real server.
414 * For LVS-NAT they must be already created. For other methods
415 * with persistence the connection is created on SYN+ACK.
416 */
417 if (pptr[0] == FTPDATA) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300418 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
419 "Not scheduling FTPDATA");
Julian Anastasov190ecd22010-10-17 16:24:37 +0300420 return NULL;
421 }
422
423 /*
Hans Schillstroma5959d52010-11-19 14:25:10 +0100424 * Do not schedule replies from local real server.
Julian Anastasov190ecd22010-10-17 16:24:37 +0300425 */
426 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200427 (cp = pp->conn_in_get(svc->af, skb, iph, 1))) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300428 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
Julian Anastasov190ecd22010-10-17 16:24:37 +0300429 "Not scheduling reply for existing connection");
430 __ip_vs_conn_put(cp);
431 return NULL;
432 }
433
434 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * Persistent service
436 */
Hans Schillstroma5959d52010-11-19 14:25:10 +0100437 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200438 return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored,
439 iph);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100440
441 *ignored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /*
444 * Non-persistent service
445 */
446 if (!svc->fwmark && pptr[1] != svc->port) {
447 if (!svc->port)
Hannes Eder1e3e2382009-08-02 11:05:41 +0000448 pr_err("Schedule: port zero only supported "
449 "in persistent services, "
450 "check your ipvs configuration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return NULL;
452 }
453
Julian Anastasovceec4c32013-03-22 11:46:53 +0200454 sched = rcu_dereference(svc->scheduler);
Julian Anastasovbba54de2013-06-16 09:09:36 +0300455 dest = sched->schedule(svc, skb, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (dest == NULL) {
457 IP_VS_DBG(1, "Schedule: no dest found.\n");
458 return NULL;
459 }
460
Nick Chalk26ec0372010-06-22 08:07:01 +0200461 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200462 && iph->protocol == IPPROTO_UDP) ?
Nick Chalk26ec0372010-06-22 08:07:01 +0200463 IP_VS_CONN_F_ONE_PACKET : 0;
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /*
466 * Create a connection entry.
467 */
Simon Hormanf11017e2010-08-22 21:37:52 +0900468 {
469 struct ip_vs_conn_param p;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100470
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200471 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
472 &iph->saddr, pptr[0], &iph->daddr,
473 pptr[1], &p);
Simon Hormanf11017e2010-08-22 21:37:52 +0900474 cp = ip_vs_conn_new(&p, &dest->addr,
475 dest->port ? dest->port : pptr[1],
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100476 flags, dest, skb->mark);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100477 if (!cp) {
478 *ignored = -1;
Simon Hormanf11017e2010-08-22 21:37:52 +0900479 return NULL;
Hans Schillstroma5959d52010-11-19 14:25:10 +0100480 }
Simon Hormanf11017e2010-08-22 21:37:52 +0900481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Julius Volzcd17f9e2008-09-02 15:55:46 +0200483 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
484 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
485 ip_vs_fwd_tag(cp),
486 IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
487 IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
488 IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
489 cp->flags, atomic_read(&cp->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 ip_vs_conn_stats(cp, svc);
492 return cp;
493}
494
495
496/*
497 * Pass or drop the packet.
498 * Called by ip_vs_in, when the virtual service is available but
499 * no destination is available for a new connection.
500 */
501int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200502 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Al Viro014d7302006-09-28 14:29:52 -0700504 __be16 _ports[2], *pptr;
Simon Hormana7a86b82011-02-04 18:33:02 +0900505#ifdef CONFIG_SYSCTL
506 struct net *net;
507 struct netns_ipvs *ipvs;
Julius Volz2a3b7912008-09-02 15:55:47 +0200508 int unicast;
Simon Hormana7a86b82011-02-04 18:33:02 +0900509#endif
Hans Schillstrom93304192011-01-03 14:44:51 +0100510
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200511 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (pptr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return NF_DROP;
514 }
Simon Hormana7a86b82011-02-04 18:33:02 +0900515
516#ifdef CONFIG_SYSCTL
Hans Schillstrom4a984802011-01-03 14:45:02 +0100517 net = skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Julius Volz2a3b7912008-09-02 15:55:47 +0200519#ifdef CONFIG_IP_VS_IPV6
520 if (svc->af == AF_INET6)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200521 unicast = ipv6_addr_type(&iph->daddr.in6) & IPV6_ADDR_UNICAST;
Julius Volz2a3b7912008-09-02 15:55:47 +0200522 else
523#endif
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200524 unicast = (inet_addr_type(net, iph->daddr.ip) == RTN_UNICAST);
Julius Volz2a3b7912008-09-02 15:55:47 +0200525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* if it is fwmark-based service, the cache_bypass sysctl is up
Julius Volz2a3b7912008-09-02 15:55:47 +0200527 and the destination is a non-local unicast, then create
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 a cache_bypass connection entry */
Hans Schillstrom4a984802011-01-03 14:45:02 +0100529 ipvs = net_ipvs(net);
Hans Schillstroma0840e22011-01-03 14:44:58 +0100530 if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) {
Krzysztof Wilczynskiad542ced2011-10-18 20:59:49 +0100531 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 struct ip_vs_conn *cp;
Julian Anastasov35757922010-09-17 14:18:16 +0200533 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200534 iph->protocol == IPPROTO_UDP) ?
Julian Anastasov35757922010-09-17 14:18:16 +0200535 IP_VS_CONN_F_ONE_PACKET : 0;
Simon Hormandff630d2008-09-17 10:10:42 +1000536 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* create a new connection entry */
Hannes Eder1e3e2382009-08-02 11:05:41 +0000539 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
Simon Hormanf11017e2010-08-22 21:37:52 +0900540 {
541 struct ip_vs_conn_param p;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200542 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
543 &iph->saddr, pptr[0],
544 &iph->daddr, pptr[1], &p);
Simon Hormanf11017e2010-08-22 21:37:52 +0900545 cp = ip_vs_conn_new(&p, &daddr, 0,
546 IP_VS_CONN_F_BYPASS | flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100547 NULL, skb->mark);
Simon Hormanf11017e2010-08-22 21:37:52 +0900548 if (!cp)
549 return NF_DROP;
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 /* statistics */
553 ip_vs_in_stats(cp, skb);
554
555 /* set state */
Simon Horman4a516f12011-09-16 14:11:49 +0900556 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* transmit the first SYN packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200559 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* do not touch skb anymore */
561
562 atomic_inc(&cp->in_pkts);
563 ip_vs_conn_put(cp);
564 return ret;
565 }
Simon Hormana7a86b82011-02-04 18:33:02 +0900566#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /*
569 * When the virtual ftp service is presented, packets destined
570 * for other services on the VIP may get here (except services
571 * listed in the ipvs table), pass the packets, because it is
572 * not ipvs job to decide to drop the packets.
573 */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200574 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /*
578 * Notify the client that the destination is unreachable, and
579 * release the socket buffer.
580 * Since it is in IP layer, the TCP socket is not actually
581 * created, the TCP RST packet cannot be sent, instead that
582 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
583 */
Julius Volz2a3b7912008-09-02 15:55:47 +0200584#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +0300585 if (svc->af == AF_INET6) {
586 if (!skb->dev) {
Simon Horman9fd0fa72013-04-19 10:25:42 +0900587 struct net *net_ = dev_net(skb_dst(skb)->dev);
Julian Anastasovcb591552010-10-17 16:40:51 +0300588
Simon Horman9fd0fa72013-04-19 10:25:42 +0900589 skb->dev = net_->loopback_dev;
Julian Anastasovcb591552010-10-17 16:40:51 +0300590 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000591 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Julian Anastasovcb591552010-10-17 16:40:51 +0300592 } else
Julius Volz2a3b7912008-09-02 15:55:47 +0200593#endif
594 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return NF_DROP;
597}
598
Simon Horman84b3cee2011-02-04 18:33:01 +0900599#ifdef CONFIG_SYSCTL
600
601static int sysctl_snat_reroute(struct sk_buff *skb)
602{
603 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
604 return ipvs->sysctl_snat_reroute;
605}
606
Simon Horman0cfa5582011-02-04 18:33:01 +0900607static int sysctl_nat_icmp_send(struct net *net)
608{
609 struct netns_ipvs *ipvs = net_ipvs(net);
610 return ipvs->sysctl_nat_icmp_send;
611}
612
Simon Horman71a8ab62011-02-04 18:33:01 +0900613static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
614{
615 return ipvs->sysctl_expire_nodest_conn;
616}
617
Simon Horman84b3cee2011-02-04 18:33:01 +0900618#else
619
620static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; }
Simon Horman0cfa5582011-02-04 18:33:01 +0900621static int sysctl_nat_icmp_send(struct net *net) { return 0; }
Simon Horman71a8ab62011-02-04 18:33:01 +0900622static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
Simon Horman84b3cee2011-02-04 18:33:01 +0900623
624#endif
625
Al Virob1550f22006-11-14 21:37:50 -0800626__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Al Virod3bc23e2006-11-14 21:24:49 -0800628 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300631static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
632{
633 if (NF_INET_LOCAL_IN == hooknum)
634 return IP_DEFRAG_VS_IN;
635 if (NF_INET_FORWARD == hooknum)
636 return IP_DEFRAG_VS_FWD;
637 return IP_DEFRAG_VS_OUT;
638}
639
Herbert Xu776c7292007-10-14 00:38:32 -0700640static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Julian Anastasovac692692013-03-22 11:46:54 +0200642 int err;
Herbert Xu776c7292007-10-14 00:38:32 -0700643
Julian Anastasovac692692013-03-22 11:46:54 +0200644 local_bh_disable();
645 err = ip_defrag(skb, user);
646 local_bh_enable();
Herbert Xu776c7292007-10-14 00:38:32 -0700647 if (!err)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700648 ip_send_check(ip_hdr(skb));
Herbert Xu776c7292007-10-14 00:38:32 -0700649
650 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900653static int ip_vs_route_me_harder(int af, struct sk_buff *skb)
654{
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900655#ifdef CONFIG_IP_VS_IPV6
656 if (af == AF_INET6) {
Simon Horman84b3cee2011-02-04 18:33:01 +0900657 if (sysctl_snat_reroute(skb) && ip6_route_me_harder(skb) != 0)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900658 return 1;
659 } else
660#endif
Simon Horman84b3cee2011-02-04 18:33:01 +0900661 if ((sysctl_snat_reroute(skb) ||
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900662 skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
663 ip_route_me_harder(skb, RTN_LOCAL) != 0)
664 return 1;
665
666 return 0;
667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/*
670 * Packet has been made sufficiently writable in caller
671 * - inout: 1=in->out, 0=out->in
672 */
673void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
674 struct ip_vs_conn *cp, int inout)
675{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700676 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 unsigned int icmp_offset = iph->ihl*4;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700678 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
679 icmp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
681
682 if (inout) {
Julius Volze7ade462008-09-02 15:55:33 +0200683 iph->saddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200685 ciph->daddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 ip_send_check(ciph);
687 } else {
Julius Volze7ade462008-09-02 15:55:33 +0200688 iph->daddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200690 ciph->saddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 ip_send_check(ciph);
692 }
693
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100694 /* the TCP/UDP/SCTP port */
695 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
696 IPPROTO_SCTP == ciph->protocol) {
Al Viro014d7302006-09-28 14:29:52 -0700697 __be16 *ports = (void *)ciph + ciph->ihl*4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 if (inout)
700 ports[1] = cp->vport;
701 else
702 ports[0] = cp->dport;
703 }
704
705 /* And finally the ICMP checksum */
706 icmph->checksum = 0;
707 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
708 skb->ip_summed = CHECKSUM_UNNECESSARY;
709
710 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300711 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 "Forwarding altered outgoing ICMP");
713 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300714 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 "Forwarding altered incoming ICMP");
716}
717
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200718#ifdef CONFIG_IP_VS_IPV6
719void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
720 struct ip_vs_conn *cp, int inout)
721{
722 struct ipv6hdr *iph = ipv6_hdr(skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200723 unsigned int icmp_offset = 0;
724 unsigned int offs = 0; /* header offset*/
725 int protocol;
726 struct icmp6hdr *icmph;
727 struct ipv6hdr *ciph;
728 unsigned short fragoffs;
729
730 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
731 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
732 offs = icmp_offset + sizeof(struct icmp6hdr);
733 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
734
735 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200736
737 if (inout) {
738 iph->saddr = cp->vaddr.in6;
739 ciph->daddr = cp->vaddr.in6;
740 } else {
741 iph->daddr = cp->daddr.in6;
742 ciph->saddr = cp->daddr.in6;
743 }
744
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100745 /* the TCP/UDP/SCTP port */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200746 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
747 IPPROTO_SCTP == protocol)) {
748 __be16 *ports = (void *)(skb_network_header(skb) + offs);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200749
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200750 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
751 ntohs(inout ? ports[1] : ports[0]),
752 ntohs(inout ? cp->vport : cp->dport));
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200753 if (inout)
754 ports[1] = cp->vport;
755 else
756 ports[0] = cp->dport;
757 }
758
759 /* And finally the ICMP checksum */
Simon Horman8870f842010-08-26 13:21:26 -0700760 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
761 skb->len - icmp_offset,
762 IPPROTO_ICMPV6, 0);
763 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
764 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
765 skb->ip_summed = CHECKSUM_PARTIAL;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200766
767 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300768 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
769 (void *)ciph - (void *)iph,
770 "Forwarding altered outgoing ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200771 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300772 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
773 (void *)ciph - (void *)iph,
774 "Forwarding altered incoming ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200775}
776#endif
777
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000778/* Handle relevant response ICMP messages - forward to the right
Julian Anastasov6cb90db2011-02-09 02:26:38 +0200779 * destination host.
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000780 */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000781static int handle_response_icmp(int af, struct sk_buff *skb,
782 union nf_inet_addr *snet,
783 __u8 protocol, struct ip_vs_conn *cp,
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000784 struct ip_vs_protocol *pp,
785 unsigned int offset, unsigned int ihl)
786{
787 unsigned int verdict = NF_DROP;
788
789 if (IP_VS_FWD_METHOD(cp) != 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000790 pr_err("shouldn't reach here, because the box is on the "
791 "half connection in the tun/dr module.\n");
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000792 }
793
794 /* Ensure the checksum is correct */
795 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
796 /* Failed checksum! */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000797 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
798 IP_VS_DBG_ADDR(af, snet));
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000799 goto out;
800 }
801
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100802 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
803 IPPROTO_SCTP == protocol)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000804 offset += 2 * sizeof(__u16);
805 if (!skb_make_writable(skb, offset))
806 goto out;
807
Simon Hormanf2428ed2008-09-05 11:17:14 +1000808#ifdef CONFIG_IP_VS_IPV6
809 if (af == AF_INET6)
810 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
811 else
812#endif
813 ip_vs_nat_icmp(skb, pp, cp, 1);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000814
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900815 if (ip_vs_route_me_harder(af, skb))
816 goto out;
Julian Anastasovf5a41842010-10-17 16:35:46 +0300817
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000818 /* do the statistics and put it back */
819 ip_vs_out_stats(cp, skb);
820
Julian Anastasovcf356d62010-10-17 16:21:07 +0300821 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200822 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +0300823 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200824 else
825 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000826 verdict = NF_ACCEPT;
827
828out:
829 __ip_vs_conn_put(cp);
830
831 return verdict;
832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/*
835 * Handle ICMP messages in the inside-to-outside direction (outgoing).
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000836 * Find any that might be relevant, check against existing connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 * Currently handles error types - unreachable, quench, ttl exceeded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 */
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300839static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
840 unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 struct iphdr *iph;
843 struct icmphdr _icmph, *ic;
844 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +0200845 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 struct ip_vs_conn *cp;
847 struct ip_vs_protocol *pp;
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000848 unsigned int offset, ihl;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000849 union nf_inet_addr snet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 *related = 1;
852
853 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -0700854 if (ip_is_fragment(ip_hdr(skb))) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300855 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700859 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 offset = ihl = iph->ihl * 4;
861 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
862 if (ic == NULL)
863 return NF_DROP;
864
Harvey Harrison14d5e832008-10-31 00:54:29 -0700865 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -0700867 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 /*
870 * Work through seeing if this is for us.
871 * These checks are supposed to be in an order that means easy
872 * things are checked first to speed up processing.... however
873 * this means that some packets will manage to get a long way
874 * down this stack and then be rejected, but that's life.
875 */
876 if ((ic->type != ICMP_DEST_UNREACH) &&
877 (ic->type != ICMP_SOURCE_QUENCH) &&
878 (ic->type != ICMP_TIME_EXCEEDED)) {
879 *related = 0;
880 return NF_ACCEPT;
881 }
882
883 /* Now find the contained IP header */
884 offset += sizeof(_icmph);
885 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
886 if (cih == NULL)
887 return NF_ACCEPT; /* The packet looks wrong, ignore */
888
889 pp = ip_vs_proto_get(cih->protocol);
890 if (!pp)
891 return NF_ACCEPT;
892
893 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900894 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 pp->dont_defrag))
896 return NF_ACCEPT;
897
Julian Anastasov0d796412010-10-17 16:46:17 +0300898 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
899 "Checking outgoing ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200901 ip_vs_fill_ip4hdr(cih, &ciph);
902 ciph.len += offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /* The embedded headers contain source and dest in reverse order */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200904 cp = pp->conn_out_get(AF_INET, skb, &ciph, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (!cp)
906 return NF_ACCEPT;
907
Simon Hormanf2428ed2008-09-05 11:17:14 +1000908 snet.ip = iph->saddr;
909 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200910 pp, ciph.len, ihl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Julius Volz2a3b7912008-09-02 15:55:47 +0200913#ifdef CONFIG_IP_VS_IPV6
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300914static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200915 unsigned int hooknum, struct ip_vs_iphdr *ipvsh)
Julius Volz2a3b7912008-09-02 15:55:47 +0200916{
Julius Volz2a3b7912008-09-02 15:55:47 +0200917 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200918 struct ipv6hdr _ip6h, *ip6h; /* The ip header contained within ICMP */
919 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +0200920 struct ip_vs_conn *cp;
921 struct ip_vs_protocol *pp;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000922 union nf_inet_addr snet;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200923 unsigned int writable;
924
Julius Volz2a3b7912008-09-02 15:55:47 +0200925 *related = 1;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200926 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
Julius Volz2a3b7912008-09-02 15:55:47 +0200927 if (ic == NULL)
928 return NF_DROP;
929
Julius Volz2a3b7912008-09-02 15:55:47 +0200930 /*
931 * Work through seeing if this is for us.
932 * These checks are supposed to be in an order that means easy
933 * things are checked first to speed up processing.... however
934 * this means that some packets will manage to get a long way
935 * down this stack and then be rejected, but that's life.
936 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +0200937 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +0200938 *related = 0;
939 return NF_ACCEPT;
940 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200941 /* Fragment header that is before ICMP header tells us that:
942 * it's not an error message since they can't be fragmented.
943 */
David S. Millere7165032012-11-30 12:01:30 -0500944 if (ipvsh->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200945 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +0200946
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200947 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
948 ic->icmp6_type, ntohs(icmpv6_id(ic)),
949 &ipvsh->saddr, &ipvsh->daddr);
Julius Volz2a3b7912008-09-02 15:55:47 +0200950
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200951 /* Now find the contained IP header */
952 ciph.len = ipvsh->len + sizeof(_icmph);
953 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
954 if (ip6h == NULL)
955 return NF_ACCEPT; /* The packet looks wrong, ignore */
956 ciph.saddr.in6 = ip6h->saddr; /* conn_out_get() handles reverse order */
957 ciph.daddr.in6 = ip6h->daddr;
958 /* skip possible IPv6 exthdrs of contained IPv6 packet */
959 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
960 if (ciph.protocol < 0)
961 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
962
963 pp = ip_vs_proto_get(ciph.protocol);
Julius Volz2a3b7912008-09-02 15:55:47 +0200964 if (!pp)
965 return NF_ACCEPT;
966
Julius Volz2a3b7912008-09-02 15:55:47 +0200967 /* The embedded headers contain source and dest in reverse order */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200968 cp = pp->conn_out_get(AF_INET6, skb, &ciph, 1);
Julius Volz2a3b7912008-09-02 15:55:47 +0200969 if (!cp)
970 return NF_ACCEPT;
971
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200972 snet.in6 = ciph.saddr.in6;
973 writable = ciph.len;
974 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
975 pp, writable, sizeof(struct ipv6hdr));
Julius Volz2a3b7912008-09-02 15:55:47 +0200976}
977#endif
978
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100979/*
980 * Check if sctp chunc is ABORT chunk
981 */
982static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
983{
984 sctp_chunkhdr_t *sch, schunk;
985 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
986 sizeof(schunk), &schunk);
987 if (sch == NULL)
988 return 0;
989 if (sch->type == SCTP_CID_ABORT)
990 return 1;
991 return 0;
992}
993
Julius Volz2a3b7912008-09-02 15:55:47 +0200994static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 struct tcphdr _tcph, *th;
997
Julius Volz2a3b7912008-09-02 15:55:47 +0200998 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (th == NULL)
1000 return 0;
1001 return th->rst;
1002}
1003
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001004static inline bool is_new_conn(const struct sk_buff *skb,
1005 struct ip_vs_iphdr *iph)
1006{
1007 switch (iph->protocol) {
1008 case IPPROTO_TCP: {
1009 struct tcphdr _tcph, *th;
1010
1011 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1012 if (th == NULL)
1013 return false;
1014 return th->syn;
1015 }
1016 case IPPROTO_SCTP: {
1017 sctp_chunkhdr_t *sch, schunk;
1018
1019 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
1020 sizeof(schunk), &schunk);
1021 if (sch == NULL)
1022 return false;
1023 return sch->type == SCTP_CID_INIT;
1024 }
1025 default:
1026 return false;
1027 }
1028}
1029
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001030/* Handle response packets: rewrite addresses and send away...
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001031 */
1032static unsigned int
Hans Schillstrom93304192011-01-03 14:44:51 +01001033handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001034 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph)
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001035{
Hans Schillstrom93304192011-01-03 14:44:51 +01001036 struct ip_vs_protocol *pp = pd->pp;
1037
Julian Anastasov0d796412010-10-17 16:46:17 +03001038 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001039
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001040 if (!skb_make_writable(skb, iph->len))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001041 goto drop;
1042
1043 /* mangle the packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001044 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001045 goto drop;
1046
1047#ifdef CONFIG_IP_VS_IPV6
1048 if (af == AF_INET6)
1049 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1050 else
1051#endif
1052 {
1053 ip_hdr(skb)->saddr = cp->vaddr.ip;
1054 ip_send_check(ip_hdr(skb));
1055 }
1056
Julian Anastasov8a803042010-09-21 17:38:57 +02001057 /*
1058 * nf_iterate does not expect change in the skb->dst->dev.
1059 * It looks like it is not fatal to enable this code for hooks
1060 * where our handlers are at the end of the chain list and
1061 * when all next handlers use skb->dst->dev and not outdev.
1062 * It will definitely route properly the inout NAT traffic
1063 * when multiple paths are used.
1064 */
1065
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001066 /* For policy routing, packets originating from this
1067 * machine itself may be routed differently to packets
1068 * passing through. We want this packet to be routed as
1069 * if it came from this machine itself. So re-compute
1070 * the routing information.
1071 */
Simon Hormanba4fd7e2011-02-04 18:33:01 +09001072 if (ip_vs_route_me_harder(af, skb))
1073 goto drop;
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001074
Julian Anastasov0d796412010-10-17 16:46:17 +03001075 IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001076
1077 ip_vs_out_stats(cp, skb);
Hans Schillstrom93304192011-01-03 14:44:51 +01001078 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
Julian Anastasovcf356d62010-10-17 16:21:07 +03001079 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001080 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +03001081 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001082 else
1083 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001084 ip_vs_conn_put(cp);
1085
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001086 LeaveFunction(11);
1087 return NF_ACCEPT;
1088
1089drop:
1090 ip_vs_conn_put(cp);
1091 kfree_skb(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001092 LeaveFunction(11);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001093 return NF_STOLEN;
1094}
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096/*
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001097 * Check if outgoing packet belongs to the established ip_vs_conn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 */
1099static unsigned int
Julian Anastasovfc604762010-10-17 16:38:15 +03001100ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Hans Schillstromfc723252011-01-03 14:44:43 +01001102 struct net *net = NULL;
Julius Volz51ef3482008-09-02 15:55:40 +02001103 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001105 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 EnterFunction(11);
1109
Julian Anastasovfc604762010-10-17 16:38:15 +03001110 /* Already marked as IPVS request or reply? */
Harald Welte6869c4d2005-08-09 19:24:19 -07001111 if (skb->ipvs_property)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return NF_ACCEPT;
1113
Julian Anastasovfc604762010-10-17 16:38:15 +03001114 /* Bad... Do not break raw sockets */
1115 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1116 af == AF_INET)) {
1117 struct sock *sk = skb->sk;
1118 struct inet_sock *inet = inet_sk(skb->sk);
1119
1120 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1121 return NF_ACCEPT;
1122 }
1123
1124 if (unlikely(!skb_dst(skb)))
1125 return NF_ACCEPT;
1126
Hans Schillstromfc723252011-01-03 14:44:43 +01001127 net = skb_net(skb);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001128 if (!net_ipvs(net)->enable)
1129 return NF_ACCEPT;
1130
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001131 ip_vs_fill_iph_skb(af, skb, &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001132#ifdef CONFIG_IP_VS_IPV6
1133 if (af == AF_INET6) {
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001134 if (!iph.fragoffs && skb_nfct_reasm(skb)) {
1135 struct sk_buff *reasm = skb_nfct_reasm(skb);
1136 /* Save fw mark for coming frags */
1137 reasm->ipvs_property = 1;
1138 reasm->mark = skb->mark;
1139 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001140 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001141 int related;
1142 int verdict = ip_vs_out_icmp_v6(skb, &related,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001143 hooknum, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Julian Anastasovf5a41842010-10-17 16:35:46 +03001145 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001146 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001147 }
1148 } else
1149#endif
1150 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001151 int related;
1152 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +02001153
Julian Anastasovf5a41842010-10-17 16:35:46 +03001154 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001155 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Hans Schillstrom93304192011-01-03 14:44:51 +01001158 pd = ip_vs_proto_data_get(net, iph.protocol);
1159 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001161 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 /* reassemble IP fragments */
Julius Volz2a3b7912008-09-02 15:55:47 +02001164#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001165 if (af == AF_INET)
Julius Volz2a3b7912008-09-02 15:55:47 +02001166#endif
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001167 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001168 if (ip_vs_gather_frags(skb,
1169 ip_vs_defrag_user(hooknum)))
Julius Volz2a3b7912008-09-02 15:55:47 +02001170 return NF_STOLEN;
1171
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001172 ip_vs_fill_ip4hdr(skb_network_header(skb), &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 /*
1176 * Check if the packet belongs to an existing entry
1177 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001178 cp = pp->conn_out_get(af, skb, &iph, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Julian Anastasovcb591552010-10-17 16:40:51 +03001180 if (likely(cp))
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001181 return handle_response(af, skb, pd, cp, &iph);
Simon Horman0cfa5582011-02-04 18:33:01 +09001182 if (sysctl_nat_icmp_send(net) &&
Julian Anastasovcb591552010-10-17 16:40:51 +03001183 (pp->protocol == IPPROTO_TCP ||
1184 pp->protocol == IPPROTO_UDP ||
1185 pp->protocol == IPPROTO_SCTP)) {
1186 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001188 pptr = frag_safe_skb_hp(skb, iph.len,
1189 sizeof(_ports), _ports, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001190 if (pptr == NULL)
1191 return NF_ACCEPT; /* Not for me */
Julian Anastasov276472e2013-03-21 11:58:08 +02001192 if (ip_vs_has_real_service(net, af, iph.protocol, &iph.saddr,
1193 pptr[0])) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001194 /*
1195 * Notify the real server: there is no
1196 * existing entry if it is not RST
1197 * packet or not TCP packet.
1198 */
1199 if ((iph.protocol != IPPROTO_TCP &&
1200 iph.protocol != IPPROTO_SCTP)
1201 || ((iph.protocol == IPPROTO_TCP
1202 && !is_tcp_reset(skb, iph.len))
1203 || (iph.protocol == IPPROTO_SCTP
1204 && !is_sctp_abort(skb,
1205 iph.len)))) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001206#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001207 if (af == AF_INET6) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001208 if (!skb->dev)
1209 skb->dev = net->loopback_dev;
1210 icmpv6_send(skb,
1211 ICMPV6_DEST_UNREACH,
1212 ICMPV6_PORT_UNREACH,
1213 0);
1214 } else
Julius Volz2a3b7912008-09-02 15:55:47 +02001215#endif
Julian Anastasovcb591552010-10-17 16:40:51 +03001216 icmp_send(skb,
1217 ICMP_DEST_UNREACH,
1218 ICMP_PORT_UNREACH, 0);
1219 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
Julian Anastasov0d796412010-10-17 16:46:17 +03001223 IP_VS_DBG_PKT(12, af, pp, skb, 0,
Julian Anastasovcb591552010-10-17 16:40:51 +03001224 "ip_vs_out: packet continues traversal as normal");
1225 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Julian Anastasovfc604762010-10-17 16:38:15 +03001228/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001229 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1230 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001231 * Check if packet is reply for established ip_vs_conn.
1232 */
1233static unsigned int
1234ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb,
1235 const struct net_device *in, const struct net_device *out,
1236 int (*okfn)(struct sk_buff *))
1237{
1238 return ip_vs_out(hooknum, skb, AF_INET);
1239}
1240
1241/*
1242 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1243 * Check if packet is reply for established ip_vs_conn.
1244 */
1245static unsigned int
1246ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb,
1247 const struct net_device *in, const struct net_device *out,
1248 int (*okfn)(struct sk_buff *))
1249{
Julian Anastasovac692692013-03-22 11:46:54 +02001250 return ip_vs_out(hooknum, skb, AF_INET);
Julian Anastasovfc604762010-10-17 16:38:15 +03001251}
1252
1253#ifdef CONFIG_IP_VS_IPV6
1254
1255/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001256 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1257 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001258 * Check if packet is reply for established ip_vs_conn.
1259 */
1260static unsigned int
1261ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb,
1262 const struct net_device *in, const struct net_device *out,
1263 int (*okfn)(struct sk_buff *))
1264{
1265 return ip_vs_out(hooknum, skb, AF_INET6);
1266}
1267
1268/*
1269 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1270 * Check if packet is reply for established ip_vs_conn.
1271 */
1272static unsigned int
1273ip_vs_local_reply6(unsigned int hooknum, struct sk_buff *skb,
1274 const struct net_device *in, const struct net_device *out,
1275 int (*okfn)(struct sk_buff *))
1276{
Julian Anastasovac692692013-03-22 11:46:54 +02001277 return ip_vs_out(hooknum, skb, AF_INET6);
Julian Anastasovfc604762010-10-17 16:38:15 +03001278}
1279
1280#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282/*
1283 * Handle ICMP messages in the outside-to-inside direction (incoming).
1284 * Find any that might be relevant, check against existing connections,
1285 * forward to the right destination host if relevant.
1286 * Currently handles error types - unreachable, quench, ttl exceeded.
1287 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001288static int
Herbert Xu3db05fe2007-10-15 00:53:15 -07001289ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Hans Schillstrom93304192011-01-03 14:44:51 +01001291 struct net *net = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 struct iphdr *iph;
1293 struct icmphdr _icmph, *ic;
1294 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +02001295 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 struct ip_vs_conn *cp;
1297 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001298 struct ip_vs_proto_data *pd;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001299 unsigned int offset, offset2, ihl, verdict;
1300 bool ipip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 *related = 1;
1303
1304 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001305 if (ip_is_fragment(ip_hdr(skb))) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001306 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001310 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 offset = ihl = iph->ihl * 4;
1312 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1313 if (ic == NULL)
1314 return NF_DROP;
1315
Harvey Harrison14d5e832008-10-31 00:54:29 -07001316 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -07001318 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 /*
1321 * Work through seeing if this is for us.
1322 * These checks are supposed to be in an order that means easy
1323 * things are checked first to speed up processing.... however
1324 * this means that some packets will manage to get a long way
1325 * down this stack and then be rejected, but that's life.
1326 */
1327 if ((ic->type != ICMP_DEST_UNREACH) &&
1328 (ic->type != ICMP_SOURCE_QUENCH) &&
1329 (ic->type != ICMP_TIME_EXCEEDED)) {
1330 *related = 0;
1331 return NF_ACCEPT;
1332 }
1333
1334 /* Now find the contained IP header */
1335 offset += sizeof(_icmph);
1336 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1337 if (cih == NULL)
1338 return NF_ACCEPT; /* The packet looks wrong, ignore */
1339
Hans Schillstrom93304192011-01-03 14:44:51 +01001340 net = skb_net(skb);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001341
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001342 /* Special case for errors for IPIP packets */
1343 ipip = false;
1344 if (cih->protocol == IPPROTO_IPIP) {
1345 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1346 return NF_ACCEPT;
1347 /* Error for our IPIP must arrive at LOCAL_IN */
1348 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1349 return NF_ACCEPT;
1350 offset += cih->ihl * 4;
1351 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1352 if (cih == NULL)
1353 return NF_ACCEPT; /* The packet looks wrong, ignore */
1354 ipip = true;
1355 }
1356
Hans Schillstrom93304192011-01-03 14:44:51 +01001357 pd = ip_vs_proto_data_get(net, cih->protocol);
1358 if (!pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001360 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +09001363 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 pp->dont_defrag))
1365 return NF_ACCEPT;
1366
Julian Anastasov0d796412010-10-17 16:46:17 +03001367 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1368 "Checking incoming ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001370 offset2 = offset;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001371 ip_vs_fill_ip4hdr(cih, &ciph);
1372 ciph.len += offset;
1373 offset = ciph.len;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001374 /* The embedded headers contain source and dest in reverse order.
1375 * For IPIP this is error for request, not for reply.
1376 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001377 cp = pp->conn_in_get(AF_INET, skb, &ciph, ipip ? 0 : 1);
Julian Anastasov6cb90db2011-02-09 02:26:38 +02001378 if (!cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return NF_ACCEPT;
1380
1381 verdict = NF_DROP;
1382
1383 /* Ensure the checksum is correct */
Herbert Xu60476372007-04-09 11:59:39 -07001384 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* Failed checksum! */
Harvey Harrison14d5e832008-10-31 00:54:29 -07001386 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1387 &iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 goto out;
1389 }
1390
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001391 if (ipip) {
1392 __be32 info = ic->un.gateway;
1393
1394 /* Update the MTU */
1395 if (ic->type == ICMP_DEST_UNREACH &&
1396 ic->code == ICMP_FRAG_NEEDED) {
1397 struct ip_vs_dest *dest = cp->dest;
1398 u32 mtu = ntohs(ic->un.frag.mtu);
1399
1400 /* Strip outer IP and ICMP, go to IPIP header */
1401 __skb_pull(skb, ihl + sizeof(_icmph));
1402 offset2 -= ihl + sizeof(_icmph);
1403 skb_reset_network_header(skb);
1404 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1405 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001406 ipv4_update_pmtu(skb, dev_net(skb->dev),
1407 mtu, 0, 0, 0, 0);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001408 /* Client uses PMTUD? */
1409 if (!(cih->frag_off & htons(IP_DF)))
1410 goto ignore_ipip;
1411 /* Prefer the resulting PMTU */
1412 if (dest) {
Julian Anastasov026ace02013-03-21 11:58:06 +02001413 struct ip_vs_dest_dst *dest_dst;
1414
1415 rcu_read_lock();
1416 dest_dst = rcu_dereference(dest->dest_dst);
1417 if (dest_dst)
1418 mtu = dst_mtu(dest_dst->dst_cache);
1419 rcu_read_unlock();
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001420 }
1421 if (mtu > 68 + sizeof(struct iphdr))
1422 mtu -= sizeof(struct iphdr);
1423 info = htonl(mtu);
1424 }
1425 /* Strip outer IP, ICMP and IPIP, go to IP header of
1426 * original request.
1427 */
1428 __skb_pull(skb, offset2);
1429 skb_reset_network_header(skb);
1430 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1431 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
1432 ic->type, ic->code, ntohl(info));
1433 icmp_send(skb, ic->type, ic->code, info);
1434 /* ICMP can be shorter but anyways, account it */
1435 ip_vs_out_stats(cp, skb);
1436
1437ignore_ipip:
1438 consume_skb(skb);
1439 verdict = NF_STOLEN;
1440 goto out;
1441 }
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* do the statistics and put it back */
1444 ip_vs_in_stats(cp, skb);
Julian Anastasov06f3d7f2013-06-18 10:08:06 +03001445 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1446 IPPROTO_SCTP == cih->protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 offset += 2 * sizeof(__u16);
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001448 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Hans Schillstrom552ad652011-06-13 12:19:26 +02001450out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 __ip_vs_conn_put(cp);
1452
1453 return verdict;
1454}
1455
Julius Volz2a3b7912008-09-02 15:55:47 +02001456#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001457static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
1458 unsigned int hooknum, struct ip_vs_iphdr *iph)
Julius Volz2a3b7912008-09-02 15:55:47 +02001459{
Hans Schillstrom93304192011-01-03 14:44:51 +01001460 struct net *net = NULL;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001461 struct ipv6hdr _ip6h, *ip6h;
Julius Volz2a3b7912008-09-02 15:55:47 +02001462 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001463 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +02001464 struct ip_vs_conn *cp;
1465 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001466 struct ip_vs_proto_data *pd;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001467 unsigned int offs_ciph, writable, verdict;
1468
Julius Volz2a3b7912008-09-02 15:55:47 +02001469 *related = 1;
1470
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001471 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001472 if (ic == NULL)
1473 return NF_DROP;
1474
Julius Volz2a3b7912008-09-02 15:55:47 +02001475 /*
1476 * Work through seeing if this is for us.
1477 * These checks are supposed to be in an order that means easy
1478 * things are checked first to speed up processing.... however
1479 * this means that some packets will manage to get a long way
1480 * down this stack and then be rejected, but that's life.
1481 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +02001482 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001483 *related = 0;
1484 return NF_ACCEPT;
1485 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001486 /* Fragment header that is before ICMP header tells us that:
1487 * it's not an error message since they can't be fragmented.
1488 */
David S. Millere7165032012-11-30 12:01:30 -05001489 if (iph->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001490 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +02001491
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001492 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1493 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1494 &iph->saddr, &iph->daddr);
1495
Julius Volz2a3b7912008-09-02 15:55:47 +02001496 /* Now find the contained IP header */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001497 ciph.len = iph->len + sizeof(_icmph);
1498 offs_ciph = ciph.len; /* Save ip header offset */
1499 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
1500 if (ip6h == NULL)
Julius Volz2a3b7912008-09-02 15:55:47 +02001501 return NF_ACCEPT; /* The packet looks wrong, ignore */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001502 ciph.saddr.in6 = ip6h->saddr; /* conn_in_get() handles reverse order */
1503 ciph.daddr.in6 = ip6h->daddr;
1504 /* skip possible IPv6 exthdrs of contained IPv6 packet */
1505 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
1506 if (ciph.protocol < 0)
1507 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
Julius Volz2a3b7912008-09-02 15:55:47 +02001508
Hans Schillstrom93304192011-01-03 14:44:51 +01001509 net = skb_net(skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001510 pd = ip_vs_proto_data_get(net, ciph.protocol);
Hans Schillstrom93304192011-01-03 14:44:51 +01001511 if (!pd)
Julius Volz2a3b7912008-09-02 15:55:47 +02001512 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001513 pp = pd->pp;
Julius Volz2a3b7912008-09-02 15:55:47 +02001514
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001515 /* Cannot handle fragmented embedded protocol */
1516 if (ciph.fragoffs)
Julius Volz2a3b7912008-09-02 15:55:47 +02001517 return NF_ACCEPT;
1518
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001519 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offs_ciph,
Julian Anastasov0d796412010-10-17 16:46:17 +03001520 "Checking incoming ICMPv6 for");
Julius Volz2a3b7912008-09-02 15:55:47 +02001521
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001522 /* The embedded headers contain source and dest in reverse order
1523 * if not from localhost
1524 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001525 cp = pp->conn_in_get(AF_INET6, skb, &ciph,
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001526 (hooknum == NF_INET_LOCAL_OUT) ? 0 : 1);
1527
Julian Anastasov6cb90db2011-02-09 02:26:38 +02001528 if (!cp)
Julius Volz2a3b7912008-09-02 15:55:47 +02001529 return NF_ACCEPT;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001530 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1531 if ((hooknum == NF_INET_LOCAL_OUT) &&
1532 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
1533 __ip_vs_conn_put(cp);
1534 return NF_ACCEPT;
1535 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001536
Julius Volz2a3b7912008-09-02 15:55:47 +02001537 /* do the statistics and put it back */
1538 ip_vs_in_stats(cp, skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001539
1540 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1541 writable = ciph.len;
1542 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1543 IPPROTO_SCTP == ciph.protocol)
1544 writable += 2 * sizeof(__u16); /* Also mangle ports */
1545
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001546 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, writable, hooknum, &ciph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001547
1548 __ip_vs_conn_put(cp);
1549
1550 return verdict;
1551}
1552#endif
1553
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/*
1556 * Check if it's for virtual services, look it up,
1557 * and send it on its way...
1558 */
1559static unsigned int
Julian Anastasovcb591552010-10-17 16:40:51 +03001560ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Hans Schillstromf1313152011-01-03 14:44:55 +01001562 struct net *net;
Julius Volz51ef3482008-09-02 15:55:40 +02001563 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001565 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 struct ip_vs_conn *cp;
Simon Horman4a516f12011-09-16 14:11:49 +09001567 int ret, pkts;
Hans Schillstromf1313152011-01-03 14:44:55 +01001568 struct netns_ipvs *ipvs;
Julius Volz51ef3482008-09-02 15:55:40 +02001569
Julian Anastasovfc604762010-10-17 16:38:15 +03001570 /* Already marked as IPVS request or reply? */
1571 if (skb->ipvs_property)
1572 return NF_ACCEPT;
1573
Julian Anastasovcb591552010-10-17 16:40:51 +03001574 /*
1575 * Big tappo:
1576 * - remote client: only PACKET_HOST
1577 * - route: used for struct net when skb->dev is unset
1578 */
1579 if (unlikely((skb->pkt_type != PACKET_HOST &&
1580 hooknum != NF_INET_LOCAL_OUT) ||
1581 !skb_dst(skb))) {
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001582 ip_vs_fill_iph_skb(af, skb, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001583 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1584 " ignored in hook %u\n",
1585 skb->pkt_type, iph.protocol,
1586 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1587 return NF_ACCEPT;
1588 }
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001589 /* ipvs enabled in this netns ? */
1590 net = skb_net(skb);
Julian Anastasov0c125822013-03-09 23:25:04 +02001591 ipvs = net_ipvs(net);
1592 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001593 return NF_ACCEPT;
1594
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001595 ip_vs_fill_iph_skb(af, skb, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Julian Anastasovcb591552010-10-17 16:40:51 +03001597 /* Bad... Do not break raw sockets */
1598 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1599 af == AF_INET)) {
1600 struct sock *sk = skb->sk;
1601 struct inet_sock *inet = inet_sk(skb->sk);
1602
1603 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1604 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606
Julius Volz94b26552009-08-31 16:22:23 +02001607#ifdef CONFIG_IP_VS_IPV6
1608 if (af == AF_INET6) {
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001609 if (!iph.fragoffs && skb_nfct_reasm(skb)) {
1610 struct sk_buff *reasm = skb_nfct_reasm(skb);
1611 /* Save fw mark for coming frags. */
1612 reasm->ipvs_property = 1;
1613 reasm->mark = skb->mark;
1614 }
Julius Volz94b26552009-08-31 16:22:23 +02001615 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001616 int related;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001617 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum,
1618 &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Julius Volz94b26552009-08-31 16:22:23 +02001620 if (related)
1621 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001622 }
1623 } else
1624#endif
1625 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001626 int related;
1627 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
Julius Volz94b26552009-08-31 16:22:23 +02001628
1629 if (related)
1630 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 /* Protocol supported? */
Hans Schillstrom93304192011-01-03 14:44:51 +01001634 pd = ip_vs_proto_data_get(net, iph.protocol);
1635 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001637 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 /*
1639 * Check if the packet belongs to an existing connection entry
1640 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001641 cp = pp->conn_in_get(af, skb, &iph, 0);
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001642
1643 if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp && cp->dest &&
1644 unlikely(!atomic_read(&cp->dest->weight)) && !iph.fragoffs &&
1645 is_new_conn(skb, &iph)) {
1646 ip_vs_conn_expire_now(cp);
1647 __ip_vs_conn_put(cp);
1648 cp = NULL;
1649 }
1650
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001651 if (unlikely(!cp) && !iph.fragoffs) {
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001652 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1653 * replayed fragment zero will already have created the cp
1654 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 int v;
1656
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001657 /* Schedule and create new connection entry into &cp */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001658 if (!pp->conn_schedule(af, skb, pd, &v, &cp, &iph))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return v;
1660 }
1661
1662 if (unlikely(!cp)) {
1663 /* sorry, all this trouble for a no-hit :) */
Julian Anastasov0d796412010-10-17 16:46:17 +03001664 IP_VS_DBG_PKT(12, af, pp, skb, 0,
Julian Anastasovcb591552010-10-17 16:40:51 +03001665 "ip_vs_in: packet continues traversal as normal");
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001666 if (iph.fragoffs && !skb_nfct_reasm(skb)) {
1667 /* Fragment that couldn't be mapped to a conn entry
1668 * and don't have any pointer to a reasm skb
1669 * 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
1728ip_vs_remote_request4(unsigned int hooknum, struct sk_buff *skb,
1729 const struct net_device *in,
1730 const struct net_device *out,
1731 int (*okfn)(struct sk_buff *))
1732{
1733 return ip_vs_in(hooknum, skb, AF_INET);
1734}
1735
1736/*
1737 * AF_INET handler in NF_INET_LOCAL_OUT chain
1738 * Schedule and forward packets from local clients
1739 */
1740static unsigned int
1741ip_vs_local_request4(unsigned int hooknum, struct sk_buff *skb,
1742 const struct net_device *in, const struct net_device *out,
1743 int (*okfn)(struct sk_buff *))
1744{
Julian Anastasovac692692013-03-22 11:46:54 +02001745 return ip_vs_in(hooknum, skb, AF_INET);
Julian Anastasovcb591552010-10-17 16:40:51 +03001746}
1747
1748#ifdef CONFIG_IP_VS_IPV6
1749
1750/*
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001751 * AF_INET6 fragment handling
1752 * Copy info from first fragment, to the rest of them.
1753 */
1754static unsigned int
1755ip_vs_preroute_frag6(unsigned int hooknum, struct sk_buff *skb,
1756 const struct net_device *in,
1757 const struct net_device *out,
1758 int (*okfn)(struct sk_buff *))
1759{
1760 struct sk_buff *reasm = skb_nfct_reasm(skb);
1761 struct net *net;
1762
1763 /* Skip if not a "replay" from nf_ct_frag6_output or first fragment.
1764 * ipvs_property is set when checking first fragment
1765 * in ip_vs_in() and ip_vs_out().
1766 */
1767 if (reasm)
1768 IP_VS_DBG(2, "Fragment recv prop:%d\n", reasm->ipvs_property);
1769 if (!reasm || !reasm->ipvs_property)
1770 return NF_ACCEPT;
1771
1772 net = skb_net(skb);
1773 if (!net_ipvs(net)->enable)
1774 return NF_ACCEPT;
1775
1776 /* Copy stored fw mark, saved in ip_vs_{in,out} */
1777 skb->mark = reasm->mark;
1778
1779 return NF_ACCEPT;
1780}
1781
1782/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001783 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1784 * Schedule and forward packets from remote clients
1785 */
1786static unsigned int
1787ip_vs_remote_request6(unsigned int hooknum, struct sk_buff *skb,
1788 const struct net_device *in,
1789 const struct net_device *out,
1790 int (*okfn)(struct sk_buff *))
1791{
1792 return ip_vs_in(hooknum, skb, AF_INET6);
1793}
1794
1795/*
1796 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1797 * Schedule and forward packets from local clients
1798 */
1799static unsigned int
1800ip_vs_local_request6(unsigned int hooknum, struct sk_buff *skb,
1801 const struct net_device *in, const struct net_device *out,
1802 int (*okfn)(struct sk_buff *))
1803{
Julian Anastasovac692692013-03-22 11:46:54 +02001804 return ip_vs_in(hooknum, skb, AF_INET6);
Julian Anastasovcb591552010-10-17 16:40:51 +03001805}
1806
1807#endif
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810/*
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001811 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 * related packets destined for 0.0.0.0/0.
1813 * When fwmark-based virtual service is used, such as transparent
1814 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1815 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001816 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 * and send them to ip_vs_in_icmp.
1818 */
1819static unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -07001820ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 const struct net_device *in, const struct net_device *out,
1822 int (*okfn)(struct sk_buff *))
1823{
1824 int r;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001825 struct net *net;
Julian Anastasov0c125822013-03-09 23:25:04 +02001826 struct netns_ipvs *ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Herbert Xu3db05fe2007-10-15 00:53:15 -07001828 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return NF_ACCEPT;
1830
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001831 /* ipvs enabled in this netns ? */
1832 net = skb_net(skb);
Julian Anastasov0c125822013-03-09 23:25:04 +02001833 ipvs = net_ipvs(net);
1834 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001835 return NF_ACCEPT;
1836
Herbert Xu3db05fe2007-10-15 00:53:15 -07001837 return ip_vs_in_icmp(skb, &r, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Julius Volz2a3b7912008-09-02 15:55:47 +02001840#ifdef CONFIG_IP_VS_IPV6
1841static unsigned int
1842ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1843 const struct net_device *in, const struct net_device *out,
1844 int (*okfn)(struct sk_buff *))
1845{
1846 int r;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001847 struct net *net;
Julian Anastasov0c125822013-03-09 23:25:04 +02001848 struct netns_ipvs *ipvs;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001849 struct ip_vs_iphdr iphdr;
Julius Volz2a3b7912008-09-02 15:55:47 +02001850
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001851 ip_vs_fill_iph_skb(AF_INET6, skb, &iphdr);
1852 if (iphdr.protocol != IPPROTO_ICMPV6)
Julius Volz2a3b7912008-09-02 15:55:47 +02001853 return NF_ACCEPT;
1854
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001855 /* ipvs enabled in this netns ? */
1856 net = skb_net(skb);
Julian Anastasov0c125822013-03-09 23:25:04 +02001857 ipvs = net_ipvs(net);
1858 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001859 return NF_ACCEPT;
1860
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001861 return ip_vs_in_icmp_v6(skb, &r, hooknum, &iphdr);
Julius Volz2a3b7912008-09-02 15:55:47 +02001862}
1863#endif
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Patrick McHardy19994142007-12-05 01:23:00 -08001866static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
Julian Anastasovcb591552010-10-17 16:40:51 +03001867 /* After packet filtering, change source only for VS/NAT */
1868 {
1869 .hook = ip_vs_reply4,
1870 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001871 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001872 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001873 .priority = NF_IP_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001874 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001875 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1876 * or VS/NAT(change destination), so that filtering rules can be
1877 * applied to IPVS. */
1878 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001879 .hook = ip_vs_remote_request4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001880 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001881 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001882 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001883 .priority = NF_IP_PRI_NAT_SRC - 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001884 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001885 /* Before ip_vs_in, change source only for VS/NAT */
Patrick McHardy41c5b312007-12-05 01:22:43 -08001886 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001887 .hook = ip_vs_local_reply4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001888 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001889 .pf = NFPROTO_IPV4,
Julian Anastasovfc604762010-10-17 16:38:15 +03001890 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001891 .priority = NF_IP_PRI_NAT_DST + 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001892 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001893 /* After mangle, schedule and forward local requests */
1894 {
1895 .hook = ip_vs_local_request4,
1896 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001897 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001898 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001899 .priority = NF_IP_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001900 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001901 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1902 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1903 {
1904 .hook = ip_vs_forward_icmp,
1905 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001906 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001907 .hooknum = NF_INET_FORWARD,
1908 .priority = 99,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001909 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001910 /* After packet filtering, change source only for VS/NAT */
1911 {
1912 .hook = ip_vs_reply4,
1913 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001914 .pf = NFPROTO_IPV4,
Julian Anastasovfc604762010-10-17 16:38:15 +03001915 .hooknum = NF_INET_FORWARD,
1916 .priority = 100,
1917 },
Julius Volz473b23d2008-09-02 15:55:54 +02001918#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001919 /* After mangle & nat fetch 2:nd fragment and following */
1920 {
1921 .hook = ip_vs_preroute_frag6,
1922 .owner = THIS_MODULE,
1923 .pf = NFPROTO_IPV6,
1924 .hooknum = NF_INET_PRE_ROUTING,
1925 .priority = NF_IP6_PRI_NAT_DST + 1,
1926 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001927 /* After packet filtering, change source only for VS/NAT */
1928 {
1929 .hook = ip_vs_reply6,
1930 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001931 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001932 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001933 .priority = NF_IP6_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001934 },
Julius Volz473b23d2008-09-02 15:55:54 +02001935 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1936 * or VS/NAT(change destination), so that filtering rules can be
1937 * applied to IPVS. */
1938 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001939 .hook = ip_vs_remote_request6,
Julius Volz473b23d2008-09-02 15:55:54 +02001940 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001941 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001942 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001943 .priority = NF_IP6_PRI_NAT_SRC - 1,
Julius Volz473b23d2008-09-02 15:55:54 +02001944 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001945 /* Before ip_vs_in, change source only for VS/NAT */
Julius Volz473b23d2008-09-02 15:55:54 +02001946 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001947 .hook = ip_vs_local_reply6,
Julius Volz473b23d2008-09-02 15:55:54 +02001948 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001949 .pf = NFPROTO_IPV4,
Julian Anastasovfc604762010-10-17 16:38:15 +03001950 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001951 .priority = NF_IP6_PRI_NAT_DST + 1,
Julius Volz473b23d2008-09-02 15:55:54 +02001952 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001953 /* After mangle, schedule and forward local requests */
1954 {
1955 .hook = ip_vs_local_request6,
1956 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001957 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001958 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001959 .priority = NF_IP6_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001960 },
Julius Volz473b23d2008-09-02 15:55:54 +02001961 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1962 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1963 {
1964 .hook = ip_vs_forward_icmp_v6,
1965 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001966 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001967 .hooknum = NF_INET_FORWARD,
1968 .priority = 99,
Julius Volz473b23d2008-09-02 15:55:54 +02001969 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001970 /* After packet filtering, change source only for VS/NAT */
1971 {
1972 .hook = ip_vs_reply6,
1973 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001974 .pf = NFPROTO_IPV6,
Julian Anastasovfc604762010-10-17 16:38:15 +03001975 .hooknum = NF_INET_FORWARD,
1976 .priority = 100,
1977 },
Julius Volz473b23d2008-09-02 15:55:54 +02001978#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979};
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001980/*
1981 * Initialize IP Virtual Server netns mem.
1982 */
1983static int __net_init __ip_vs_init(struct net *net)
1984{
1985 struct netns_ipvs *ipvs;
1986
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001987 ipvs = net_generic(net, ip_vs_net_id);
Joe Perches0a9ee812011-08-29 14:17:25 -07001988 if (ipvs == NULL)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001989 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -07001990
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001991 /* Hold the beast until a service is registerd */
1992 ipvs->enable = 0;
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001993 ipvs->net = net;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001994 /* Counters used for creating unique names */
1995 ipvs->gen = atomic_read(&ipvs_netns_cnt);
1996 atomic_inc(&ipvs_netns_cnt);
1997 net->ipvs = ipvs;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001998
Hans Schillstrom503cf152011-05-01 18:50:16 +02001999 if (ip_vs_estimator_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002000 goto estimator_fail;
2001
Hans Schillstrom503cf152011-05-01 18:50:16 +02002002 if (ip_vs_control_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002003 goto control_fail;
2004
Hans Schillstrom503cf152011-05-01 18:50:16 +02002005 if (ip_vs_protocol_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002006 goto protocol_fail;
2007
Hans Schillstrom503cf152011-05-01 18:50:16 +02002008 if (ip_vs_app_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002009 goto app_fail;
2010
Hans Schillstrom503cf152011-05-01 18:50:16 +02002011 if (ip_vs_conn_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002012 goto conn_fail;
2013
Hans Schillstrom503cf152011-05-01 18:50:16 +02002014 if (ip_vs_sync_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002015 goto sync_fail;
2016
Simon Hormana870c8c2011-02-01 18:21:53 +01002017 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002018 sizeof(struct netns_ipvs), ipvs->gen);
2019 return 0;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002020/*
2021 * Error handling
2022 */
2023
2024sync_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002025 ip_vs_conn_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002026conn_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002027 ip_vs_app_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002028app_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002029 ip_vs_protocol_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002030protocol_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002031 ip_vs_control_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002032control_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002033 ip_vs_estimator_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002034estimator_fail:
Julian Anastasov39f618b2012-04-25 00:29:58 +03002035 net->ipvs = NULL;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002036 return -ENOMEM;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002037}
2038
2039static void __net_exit __ip_vs_cleanup(struct net *net)
2040{
Hans Schillstrom503cf152011-05-01 18:50:16 +02002041 ip_vs_service_net_cleanup(net); /* ip_vs_flush() with locks */
2042 ip_vs_conn_net_cleanup(net);
2043 ip_vs_app_net_cleanup(net);
2044 ip_vs_protocol_net_cleanup(net);
2045 ip_vs_control_net_cleanup(net);
2046 ip_vs_estimator_net_cleanup(net);
Hans Schillstrom1ae132b2011-05-03 22:09:30 +02002047 IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net)->gen);
Julian Anastasov39f618b2012-04-25 00:29:58 +03002048 net->ipvs = NULL;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002049}
2050
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002051static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2052{
2053 EnterFunction(2);
2054 net_ipvs(net)->enable = 0; /* Disable packet reception */
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +02002055 smp_wmb();
Hans Schillstrom503cf152011-05-01 18:50:16 +02002056 ip_vs_sync_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002057 LeaveFunction(2);
2058}
2059
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002060static struct pernet_operations ipvs_core_ops = {
2061 .init = __ip_vs_init,
2062 .exit = __ip_vs_cleanup,
2063 .id = &ip_vs_net_id,
2064 .size = sizeof(struct netns_ipvs),
2065};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002067static struct pernet_operations ipvs_core_dev_ops = {
2068 .exit = __ip_vs_dev_cleanup,
2069};
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071/*
2072 * Initialize IP Virtual Server
2073 */
2074static int __init ip_vs_init(void)
2075{
2076 int ret;
2077
2078 ret = ip_vs_control_init();
2079 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002080 pr_err("can't setup control.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002081 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
2083
2084 ip_vs_protocol_init();
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 ret = ip_vs_conn_init();
2087 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002088 pr_err("can't setup connection table.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002089 goto cleanup_protocol;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002090 }
2091
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002092 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2093 if (ret < 0)
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002094 goto cleanup_conn;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002095
2096 ret = register_pernet_device(&ipvs_core_dev_ops);
2097 if (ret < 0)
2098 goto cleanup_sub;
2099
Patrick McHardy41c5b312007-12-05 01:22:43 -08002100 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002102 pr_err("can't register hooks.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002103 goto cleanup_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105
Hans Schillstrom8537de82012-04-26 07:47:44 +02002106 ret = ip_vs_register_nl_ioctl();
2107 if (ret < 0) {
2108 pr_err("can't register netlink/ioctl.\n");
2109 goto cleanup_hooks;
2110 }
2111
Hannes Eder1e3e2382009-08-02 11:05:41 +00002112 pr_info("ipvs loaded.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return ret;
2115
Hans Schillstrom8537de82012-04-26 07:47:44 +02002116cleanup_hooks:
2117 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002118cleanup_dev:
2119 unregister_pernet_device(&ipvs_core_dev_ops);
2120cleanup_sub:
2121 unregister_pernet_subsys(&ipvs_core_ops);
Hans Schillstrom552ad652011-06-13 12:19:26 +02002122cleanup_conn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 ip_vs_conn_cleanup();
Hans Schillstrom552ad652011-06-13 12:19:26 +02002124cleanup_protocol:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 ip_vs_protocol_cleanup();
2126 ip_vs_control_cleanup();
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002127exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return ret;
2129}
2130
2131static void __exit ip_vs_cleanup(void)
2132{
Hans Schillstrom8537de82012-04-26 07:47:44 +02002133 ip_vs_unregister_nl_ioctl();
Patrick McHardy41c5b312007-12-05 01:22:43 -08002134 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002135 unregister_pernet_device(&ipvs_core_dev_ops);
2136 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 ip_vs_conn_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 ip_vs_protocol_cleanup();
2139 ip_vs_control_cleanup();
Hannes Eder1e3e2382009-08-02 11:05:41 +00002140 pr_info("ipvs unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
2143module_init(ip_vs_init);
2144module_exit(ip_vs_cleanup);
2145MODULE_LICENSE("GPL");