blob: b87ca32efa0b4e6edc7f251c2c32c4ba3b55659c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPVS An implementation of the IP virtual server support for the
3 * LINUX operating system. IPVS is now implemented as a module
4 * over the Netfilter framework. IPVS can be used to build a
5 * high-performance and highly available server based on a
6 * cluster of servers.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
9 * Peter Kese <peter.kese@ijs.si>
10 * Julian Anastasov <ja@ssi.bg>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 * The IPVS code for kernel 2.2 was done by Wensong Zhang and Peter Kese,
18 * with changes/fixes from Julian Anastasov, Lars Marowsky-Bree, Horms
19 * and others.
20 *
21 * Changes:
22 * Paul `Rusty' Russell properly handle non-linear skbs
Harald Welte6869c4d2005-08-09 19:24:19 -070023 * Harald Welte don't use nfcache
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 *
25 */
26
Hannes Eder9aada7a2009-07-30 14:29:44 -070027#define KMSG_COMPONENT "IPVS"
28#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/ip.h>
33#include <linux/tcp.h>
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +010034#include <linux/sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/icmp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <net/ip.h>
39#include <net/tcp.h>
40#include <net/udp.h>
41#include <net/icmp.h> /* for icmp_send */
42#include <net/route.h>
Stephen Rothwell2c70b512010-08-29 17:04:53 +000043#include <net/ip6_checksum.h>
Hans Schillstrom61b1ab42011-01-03 14:44:42 +010044#include <net/netns/generic.h> /* net_generic() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <linux/netfilter.h>
47#include <linux/netfilter_ipv4.h>
48
Julius Volz2a3b7912008-09-02 15:55:47 +020049#ifdef CONFIG_IP_VS_IPV6
50#include <net/ipv6.h>
51#include <linux/netfilter_ipv6.h>
Julian Anastasov489fded2010-10-17 16:27:31 +030052#include <net/ip6_route.h>
Julius Volz2a3b7912008-09-02 15:55:47 +020053#endif
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/ip_vs.h>
56
57
58EXPORT_SYMBOL(register_ip_vs_scheduler);
59EXPORT_SYMBOL(unregister_ip_vs_scheduler);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060EXPORT_SYMBOL(ip_vs_proto_name);
61EXPORT_SYMBOL(ip_vs_conn_new);
62EXPORT_SYMBOL(ip_vs_conn_in_get);
63EXPORT_SYMBOL(ip_vs_conn_out_get);
64#ifdef CONFIG_IP_VS_PROTO_TCP
65EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
66#endif
67EXPORT_SYMBOL(ip_vs_conn_put);
68#ifdef CONFIG_IP_VS_DEBUG
69EXPORT_SYMBOL(ip_vs_get_debug_level);
70#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Julian Anastasovb962abd2013-03-09 23:25:08 +020072static int ip_vs_net_id __read_mostly;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +010073/* netns cnt used for uniqueness */
74static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/* ID used in ICMP lookups */
77#define icmp_id(icmph) (((icmph)->un).echo.id)
Julius Volz2a3b7912008-09-02 15:55:47 +020078#define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Eric Dumazet95c96172012-04-15 05:58:06 +000080const char *ip_vs_proto_name(unsigned int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 static char buf[20];
83
84 switch (proto) {
85 case IPPROTO_IP:
86 return "IP";
87 case IPPROTO_UDP:
88 return "UDP";
89 case IPPROTO_TCP:
90 return "TCP";
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +010091 case IPPROTO_SCTP:
92 return "SCTP";
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 case IPPROTO_ICMP:
94 return "ICMP";
Julius Volz2a3b7912008-09-02 15:55:47 +020095#ifdef CONFIG_IP_VS_IPV6
96 case IPPROTO_ICMPV6:
97 return "ICMPv6";
98#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 default:
Masanari Iida1404c3a2014-04-21 09:16:47 +0900100 sprintf(buf, "IP_%u", proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return buf;
102 }
103}
104
105void ip_vs_init_hash_table(struct list_head *table, int rows)
106{
107 while (--rows >= 0)
108 INIT_LIST_HEAD(&table[rows]);
109}
110
111static inline void
112ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
113{
114 struct ip_vs_dest *dest = cp->dest;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100115 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
Hans Schillstromb17fc992011-01-03 14:44:56 +0100118 struct ip_vs_cpu_stats *s;
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300119 struct ip_vs_service *svc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Hans Schillstromb17fc992011-01-03 14:44:56 +0100121 s = this_cpu_ptr(dest->stats.cpustats);
122 s->ustats.inpkts++;
123 u64_stats_update_begin(&s->syncp);
124 s->ustats.inbytes += skb->len;
125 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300127 rcu_read_lock();
128 svc = rcu_dereference(dest->svc);
129 s = this_cpu_ptr(svc->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100130 s->ustats.inpkts++;
131 u64_stats_update_begin(&s->syncp);
132 s->ustats.inbytes += skb->len;
133 u64_stats_update_end(&s->syncp);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300134 rcu_read_unlock();
Hans Schillstromb17fc992011-01-03 14:44:56 +0100135
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200136 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100137 s->ustats.inpkts++;
138 u64_stats_update_begin(&s->syncp);
139 s->ustats.inbytes += skb->len;
140 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142}
143
144
145static inline void
146ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
147{
148 struct ip_vs_dest *dest = cp->dest;
Hans Schillstromb17fc992011-01-03 14:44:56 +0100149 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
Hans Schillstromb17fc992011-01-03 14:44:56 +0100152 struct ip_vs_cpu_stats *s;
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300153 struct ip_vs_service *svc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Hans Schillstromb17fc992011-01-03 14:44:56 +0100155 s = this_cpu_ptr(dest->stats.cpustats);
156 s->ustats.outpkts++;
157 u64_stats_update_begin(&s->syncp);
158 s->ustats.outbytes += skb->len;
159 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300161 rcu_read_lock();
162 svc = rcu_dereference(dest->svc);
163 s = this_cpu_ptr(svc->stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100164 s->ustats.outpkts++;
165 u64_stats_update_begin(&s->syncp);
166 s->ustats.outbytes += skb->len;
167 u64_stats_update_end(&s->syncp);
Julian Anastasovbcbde4c2013-09-12 11:21:07 +0300168 rcu_read_unlock();
Hans Schillstromb17fc992011-01-03 14:44:56 +0100169
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200170 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100171 s->ustats.outpkts++;
172 u64_stats_update_begin(&s->syncp);
173 s->ustats.outbytes += skb->len;
174 u64_stats_update_end(&s->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176}
177
178
179static inline void
180ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
181{
Hans Schillstromb17fc992011-01-03 14:44:56 +0100182 struct netns_ipvs *ipvs = net_ipvs(svc->net);
183 struct ip_vs_cpu_stats *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Hans Schillstromb17fc992011-01-03 14:44:56 +0100185 s = this_cpu_ptr(cp->dest->stats.cpustats);
186 s->ustats.conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Hans Schillstromb17fc992011-01-03 14:44:56 +0100188 s = this_cpu_ptr(svc->stats.cpustats);
189 s->ustats.conns++;
190
Julian Anastasov2a0751a2011-03-04 12:20:35 +0200191 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
Hans Schillstromb17fc992011-01-03 14:44:56 +0100192 s->ustats.conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195
Simon Horman4a516f12011-09-16 14:11:49 +0900196static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197ip_vs_set_state(struct ip_vs_conn *cp, int direction,
198 const struct sk_buff *skb,
Hans Schillstrom93304192011-01-03 14:44:51 +0100199 struct ip_vs_proto_data *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Simon Horman4a516f12011-09-16 14:11:49 +0900201 if (likely(pd->pp->state_transition))
202 pd->pp->state_transition(cp, direction, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Hans Schillstroma5959d52010-11-19 14:25:10 +0100205static inline int
Simon Horman85999282010-08-22 21:37:53 +0900206ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
207 struct sk_buff *skb, int protocol,
208 const union nf_inet_addr *caddr, __be16 cport,
209 const union nf_inet_addr *vaddr, __be16 vport,
210 struct ip_vs_conn_param *p)
211{
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100212 ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr,
213 vport, p);
Julian Anastasovceec4c32013-03-22 11:46:53 +0200214 p->pe = rcu_dereference(svc->pe);
Simon Horman85999282010-08-22 21:37:53 +0900215 if (p->pe && p->pe->fill_param)
Hans Schillstroma5959d52010-11-19 14:25:10 +0100216 return p->pe->fill_param(p, skb);
217
218 return 0;
Simon Horman85999282010-08-22 21:37:53 +0900219}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
222 * IPVS persistent scheduling function
223 * It creates a connection entry according to its template if exists,
224 * or selects a server and creates a connection entry plus a template.
225 * Locking: we are svc user (svc->refcnt), so we hold all dests too
226 * Protocols supported: TCP, UDP
227 */
228static struct ip_vs_conn *
229ip_vs_sched_persist(struct ip_vs_service *svc,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200230 struct sk_buff *skb, __be16 src_port, __be16 dst_port,
231 int *ignored, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct ip_vs_conn *cp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 struct ip_vs_dest *dest;
235 struct ip_vs_conn *ct;
Simon Horman5b57a982010-08-22 21:37:51 +0900236 __be16 dport = 0; /* destination port to forward */
Julian Anastasov35757922010-09-17 14:18:16 +0200237 unsigned int flags;
Simon Hormanf11017e2010-08-22 21:37:52 +0900238 struct ip_vs_conn_param param;
Simon Hormane0aac522012-01-27 10:45:27 +0900239 const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
Julius Volz28364a52008-09-02 15:55:43 +0200240 union nf_inet_addr snet; /* source network of the client,
241 after masking */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* Mask saddr with the netmask to adjust template granularity */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200244#ifdef CONFIG_IP_VS_IPV6
245 if (svc->af == AF_INET6)
Julian Anastasov0a925862013-04-17 23:50:49 +0300246 ipv6_addr_prefix(&snet.in6, &iph->saddr.in6,
247 (__force __u32) svc->netmask);
Julius Volzcd17f9e2008-09-02 15:55:46 +0200248 else
249#endif
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200250 snet.ip = iph->saddr.ip & svc->netmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Julius Volzcd17f9e2008-09-02 15:55:46 +0200252 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
253 "mnet %s\n",
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200254 IP_VS_DBG_ADDR(svc->af, &iph->saddr), ntohs(src_port),
255 IP_VS_DBG_ADDR(svc->af, &iph->daddr), ntohs(dst_port),
Julius Volzcd17f9e2008-09-02 15:55:46 +0200256 IP_VS_DBG_ADDR(svc->af, &snet));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /*
259 * As far as we know, FTP is a very complicated network protocol, and
260 * it uses control connection and data connections. For active FTP,
261 * FTP server initialize data connection to the client, its source port
262 * is often 20. For passive FTP, FTP server tells the clients the port
263 * that it passively listens to, and the client issues the data
264 * connection. In the tunneling or direct routing mode, the load
265 * balancer is on the client-to-server half of connection, the port
266 * number is unknown to the load balancer. So, a conn template like
267 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
268 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
269 * is created for other persistent services.
270 */
Simon Horman5b57a982010-08-22 21:37:51 +0900271 {
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200272 int protocol = iph->protocol;
273 const union nf_inet_addr *vaddr = &iph->daddr;
Simon Hormanf11017e2010-08-22 21:37:52 +0900274 __be16 vport = 0;
275
Hans Schillstromce144f22010-11-19 14:25:08 +0100276 if (dst_port == svc->port) {
Simon Horman5b57a982010-08-22 21:37:51 +0900277 /* non-FTP template:
278 * <protocol, caddr, 0, vaddr, vport, daddr, dport>
279 * FTP template:
280 * <protocol, caddr, 0, vaddr, 0, daddr, 0>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
282 if (svc->port != FTPPORT)
Hans Schillstromce144f22010-11-19 14:25:08 +0100283 vport = dst_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900285 /* Note: persistent fwmark-based services and
286 * persistent port zero service are handled here.
287 * fwmark template:
288 * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
289 * port zero template:
290 * <protocol,caddr,0,vaddr,0,daddr,0>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
Julius Volz28364a52008-09-02 15:55:43 +0200292 if (svc->fwmark) {
Simon Horman5b57a982010-08-22 21:37:51 +0900293 protocol = IPPROTO_IP;
294 vaddr = &fwmark;
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Hans Schillstroma5959d52010-11-19 14:25:10 +0100297 /* return *ignored = -1 so NF_DROP can be used */
298 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
299 vaddr, vport, &param) < 0) {
300 *ignored = -1;
301 return NULL;
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
Simon Horman5b57a982010-08-22 21:37:51 +0900305 /* Check if a template already exists */
Simon Hormanf11017e2010-08-22 21:37:52 +0900306 ct = ip_vs_ct_in_get(&param);
Simon Horman5b57a982010-08-22 21:37:51 +0900307 if (!ct || !ip_vs_check_template(ct)) {
Julian Anastasovceec4c32013-03-22 11:46:53 +0200308 struct ip_vs_scheduler *sched;
309
Hans Schillstroma5959d52010-11-19 14:25:10 +0100310 /*
311 * No template found or the dest of the connection
Simon Horman5b57a982010-08-22 21:37:51 +0900312 * template is not available.
Hans Schillstroma5959d52010-11-19 14:25:10 +0100313 * return *ignored=0 i.e. ICMP and NF_DROP
Simon Horman5b57a982010-08-22 21:37:51 +0900314 */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200315 sched = rcu_dereference(svc->scheduler);
Julian Anastasovbba54de2013-06-16 09:09:36 +0300316 dest = sched->schedule(svc, skb, iph);
Simon Horman5b57a982010-08-22 21:37:51 +0900317 if (!dest) {
318 IP_VS_DBG(1, "p-schedule: no dest found.\n");
Simon Horman85999282010-08-22 21:37:53 +0900319 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100320 *ignored = 0;
Simon Horman5b57a982010-08-22 21:37:51 +0900321 return NULL;
322 }
323
Hans Schillstromce144f22010-11-19 14:25:08 +0100324 if (dst_port == svc->port && svc->port != FTPPORT)
Simon Horman5b57a982010-08-22 21:37:51 +0900325 dport = dest->port;
326
Simon Horman85999282010-08-22 21:37:53 +0900327 /* Create a template
328 * This adds param.pe_data to the template,
329 * and thus param.pe_data will be destroyed
330 * when the template expires */
Alex Gartrellba385282014-09-09 16:40:23 -0700331 ct = ip_vs_conn_new(&param, dest->af, &dest->addr, dport,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100332 IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
Simon Horman85999282010-08-22 21:37:53 +0900333 if (ct == NULL) {
334 kfree(param.pe_data);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100335 *ignored = -1;
Simon Horman5b57a982010-08-22 21:37:51 +0900336 return NULL;
Simon Horman85999282010-08-22 21:37:53 +0900337 }
Simon Horman5b57a982010-08-22 21:37:51 +0900338
339 ct->timeout = svc->timeout;
Simon Horman85999282010-08-22 21:37:53 +0900340 } else {
Simon Horman5b57a982010-08-22 21:37:51 +0900341 /* set destination with the found template */
342 dest = ct->dest;
Simon Horman85999282010-08-22 21:37:53 +0900343 kfree(param.pe_data);
344 }
Simon Horman5b57a982010-08-22 21:37:51 +0900345
Hans Schillstromce144f22010-11-19 14:25:08 +0100346 dport = dst_port;
Simon Horman5b57a982010-08-22 21:37:51 +0900347 if (dport == svc->port && dest->port)
348 dport = dest->port;
349
Nick Chalk26ec0372010-06-22 08:07:01 +0200350 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200351 && iph->protocol == IPPROTO_UDP) ?
Nick Chalk26ec0372010-06-22 08:07:01 +0200352 IP_VS_CONN_F_ONE_PACKET : 0;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /*
355 * Create a new connection according to the template
356 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200357 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol, &iph->saddr,
358 src_port, &iph->daddr, dst_port, &param);
Hans Schillstromce144f22010-11-19 14:25:08 +0100359
Alex Gartrellba385282014-09-09 16:40:23 -0700360 cp = ip_vs_conn_new(&param, dest->af, &dest->addr, dport, flags, dest,
361 skb->mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (cp == NULL) {
363 ip_vs_conn_put(ct);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100364 *ignored = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return NULL;
366 }
367
368 /*
369 * Add its control
370 */
371 ip_vs_control_add(cp, ct);
372 ip_vs_conn_put(ct);
373
374 ip_vs_conn_stats(cp, svc);
375 return cp;
376}
377
378
379/*
380 * IPVS main scheduling function
381 * It selects a server according to the virtual service, and
382 * creates a connection entry.
383 * Protocols supported: TCP, UDP
Hans Schillstroma5959d52010-11-19 14:25:10 +0100384 *
385 * Usage of *ignored
386 *
387 * 1 : protocol tried to schedule (eg. on SYN), found svc but the
388 * svc/scheduler decides that this packet should be accepted with
389 * NF_ACCEPT because it must not be scheduled.
390 *
391 * 0 : scheduler can not find destination, so try bypass or
392 * return ICMP and then NF_DROP (ip_vs_leave).
393 *
394 * -1 : scheduler tried to schedule but fatal error occurred, eg.
395 * ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
396 * failure such as missing Call-ID, ENOMEM on skb_linearize
397 * or pe_data. In this case we should return NF_DROP without
398 * any attempts to send ICMP with ip_vs_leave.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
400struct ip_vs_conn *
Julian Anastasov190ecd22010-10-17 16:24:37 +0300401ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200402 struct ip_vs_proto_data *pd, int *ignored,
403 struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Hans Schillstrom93304192011-01-03 14:44:51 +0100405 struct ip_vs_protocol *pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 struct ip_vs_conn *cp = NULL;
Julian Anastasovceec4c32013-03-22 11:46:53 +0200407 struct ip_vs_scheduler *sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct ip_vs_dest *dest;
Julian Anastasov35757922010-09-17 14:18:16 +0200409 __be16 _ports[2], *pptr;
410 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Julian Anastasov190ecd22010-10-17 16:24:37 +0300412 *ignored = 1;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200413 /*
414 * IPv6 frags, only the first hit here.
415 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200416 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (pptr == NULL)
418 return NULL;
419
420 /*
Julian Anastasov190ecd22010-10-17 16:24:37 +0300421 * FTPDATA needs this check when using local real server.
422 * Never schedule Active FTPDATA connections from real server.
423 * For LVS-NAT they must be already created. For other methods
424 * with persistence the connection is created on SYN+ACK.
425 */
426 if (pptr[0] == FTPDATA) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300427 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
428 "Not scheduling FTPDATA");
Julian Anastasov190ecd22010-10-17 16:24:37 +0300429 return NULL;
430 }
431
432 /*
Hans Schillstroma5959d52010-11-19 14:25:10 +0100433 * Do not schedule replies from local real server.
Julian Anastasov190ecd22010-10-17 16:24:37 +0300434 */
435 if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200436 (cp = pp->conn_in_get(svc->af, skb, iph, 1))) {
Julian Anastasov0d796412010-10-17 16:46:17 +0300437 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
Julian Anastasov190ecd22010-10-17 16:24:37 +0300438 "Not scheduling reply for existing connection");
439 __ip_vs_conn_put(cp);
440 return NULL;
441 }
442
443 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * Persistent service
445 */
Hans Schillstroma5959d52010-11-19 14:25:10 +0100446 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200447 return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored,
448 iph);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100449
450 *ignored = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /*
453 * Non-persistent service
454 */
455 if (!svc->fwmark && pptr[1] != svc->port) {
456 if (!svc->port)
Hannes Eder1e3e2382009-08-02 11:05:41 +0000457 pr_err("Schedule: port zero only supported "
458 "in persistent services, "
459 "check your ipvs configuration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return NULL;
461 }
462
Julian Anastasovceec4c32013-03-22 11:46:53 +0200463 sched = rcu_dereference(svc->scheduler);
Julian Anastasovbba54de2013-06-16 09:09:36 +0300464 dest = sched->schedule(svc, skb, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (dest == NULL) {
466 IP_VS_DBG(1, "Schedule: no dest found.\n");
467 return NULL;
468 }
469
Nick Chalk26ec0372010-06-22 08:07:01 +0200470 flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200471 && iph->protocol == IPPROTO_UDP) ?
Nick Chalk26ec0372010-06-22 08:07:01 +0200472 IP_VS_CONN_F_ONE_PACKET : 0;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /*
475 * Create a connection entry.
476 */
Simon Hormanf11017e2010-08-22 21:37:52 +0900477 {
478 struct ip_vs_conn_param p;
Hans Schillstrom6e67e582011-01-03 14:44:57 +0100479
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200480 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
481 &iph->saddr, pptr[0], &iph->daddr,
482 pptr[1], &p);
Alex Gartrellba385282014-09-09 16:40:23 -0700483 cp = ip_vs_conn_new(&p, dest->af, &dest->addr,
Simon Hormanf11017e2010-08-22 21:37:52 +0900484 dest->port ? dest->port : pptr[1],
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100485 flags, dest, skb->mark);
Hans Schillstroma5959d52010-11-19 14:25:10 +0100486 if (!cp) {
487 *ignored = -1;
Simon Hormanf11017e2010-08-22 21:37:52 +0900488 return NULL;
Hans Schillstroma5959d52010-11-19 14:25:10 +0100489 }
Simon Hormanf11017e2010-08-22 21:37:52 +0900490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Julius Volzcd17f9e2008-09-02 15:55:46 +0200492 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
493 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
494 ip_vs_fwd_tag(cp),
Julian Anastasovf18ae722014-09-09 16:40:38 -0700495 IP_VS_DBG_ADDR(cp->af, &cp->caddr), ntohs(cp->cport),
496 IP_VS_DBG_ADDR(cp->af, &cp->vaddr), ntohs(cp->vport),
497 IP_VS_DBG_ADDR(cp->daf, &cp->daddr), ntohs(cp->dport),
Julius Volzcd17f9e2008-09-02 15:55:46 +0200498 cp->flags, atomic_read(&cp->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 ip_vs_conn_stats(cp, svc);
501 return cp;
502}
503
504
505/*
506 * Pass or drop the packet.
507 * Called by ip_vs_in, when the virtual service is available but
508 * no destination is available for a new connection.
509 */
510int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200511 struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Al Viro014d7302006-09-28 14:29:52 -0700513 __be16 _ports[2], *pptr;
Simon Hormana7a86b82011-02-04 18:33:02 +0900514#ifdef CONFIG_SYSCTL
515 struct net *net;
516 struct netns_ipvs *ipvs;
Julius Volz2a3b7912008-09-02 15:55:47 +0200517 int unicast;
Simon Hormana7a86b82011-02-04 18:33:02 +0900518#endif
Hans Schillstrom93304192011-01-03 14:44:51 +0100519
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200520 pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (pptr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return NF_DROP;
523 }
Simon Hormana7a86b82011-02-04 18:33:02 +0900524
525#ifdef CONFIG_SYSCTL
Hans Schillstrom4a984802011-01-03 14:45:02 +0100526 net = skb_net(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Julius Volz2a3b7912008-09-02 15:55:47 +0200528#ifdef CONFIG_IP_VS_IPV6
529 if (svc->af == AF_INET6)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200530 unicast = ipv6_addr_type(&iph->daddr.in6) & IPV6_ADDR_UNICAST;
Julius Volz2a3b7912008-09-02 15:55:47 +0200531 else
532#endif
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200533 unicast = (inet_addr_type(net, iph->daddr.ip) == RTN_UNICAST);
Julius Volz2a3b7912008-09-02 15:55:47 +0200534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* if it is fwmark-based service, the cache_bypass sysctl is up
Julius Volz2a3b7912008-09-02 15:55:47 +0200536 and the destination is a non-local unicast, then create
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 a cache_bypass connection entry */
Hans Schillstrom4a984802011-01-03 14:45:02 +0100538 ipvs = net_ipvs(net);
Hans Schillstroma0840e22011-01-03 14:44:58 +0100539 if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) {
Krzysztof Wilczynskiad542ced2011-10-18 20:59:49 +0100540 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct ip_vs_conn *cp;
Julian Anastasov35757922010-09-17 14:18:16 +0200542 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200543 iph->protocol == IPPROTO_UDP) ?
Julian Anastasov35757922010-09-17 14:18:16 +0200544 IP_VS_CONN_F_ONE_PACKET : 0;
Simon Hormandff630d2008-09-17 10:10:42 +1000545 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* create a new connection entry */
Hannes Eder1e3e2382009-08-02 11:05:41 +0000548 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
Simon Hormanf11017e2010-08-22 21:37:52 +0900549 {
550 struct ip_vs_conn_param p;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200551 ip_vs_conn_fill_param(svc->net, svc->af, iph->protocol,
552 &iph->saddr, pptr[0],
553 &iph->daddr, pptr[1], &p);
Alex Gartrellba385282014-09-09 16:40:23 -0700554 cp = ip_vs_conn_new(&p, svc->af, &daddr, 0,
Simon Hormanf11017e2010-08-22 21:37:52 +0900555 IP_VS_CONN_F_BYPASS | flags,
Hans Schillstrom0e051e62010-11-19 14:25:07 +0100556 NULL, skb->mark);
Simon Hormanf11017e2010-08-22 21:37:52 +0900557 if (!cp)
558 return NF_DROP;
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /* statistics */
562 ip_vs_in_stats(cp, skb);
563
564 /* set state */
Simon Horman4a516f12011-09-16 14:11:49 +0900565 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* transmit the first SYN packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200568 ret = cp->packet_xmit(skb, cp, pd->pp, iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* do not touch skb anymore */
570
571 atomic_inc(&cp->in_pkts);
572 ip_vs_conn_put(cp);
573 return ret;
574 }
Simon Hormana7a86b82011-02-04 18:33:02 +0900575#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /*
578 * When the virtual ftp service is presented, packets destined
579 * for other services on the VIP may get here (except services
580 * listed in the ipvs table), pass the packets, because it is
581 * not ipvs job to decide to drop the packets.
582 */
Julian Anastasovceec4c32013-03-22 11:46:53 +0200583 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /*
587 * Notify the client that the destination is unreachable, and
588 * release the socket buffer.
589 * Since it is in IP layer, the TCP socket is not actually
590 * created, the TCP RST packet cannot be sent, instead that
591 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
592 */
Julius Volz2a3b7912008-09-02 15:55:47 +0200593#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +0300594 if (svc->af == AF_INET6) {
595 if (!skb->dev) {
Simon Horman9fd0fa72013-04-19 10:25:42 +0900596 struct net *net_ = dev_net(skb_dst(skb)->dev);
Julian Anastasovcb591552010-10-17 16:40:51 +0300597
Simon Horman9fd0fa72013-04-19 10:25:42 +0900598 skb->dev = net_->loopback_dev;
Julian Anastasovcb591552010-10-17 16:40:51 +0300599 }
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000600 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
Julian Anastasovcb591552010-10-17 16:40:51 +0300601 } else
Julius Volz2a3b7912008-09-02 15:55:47 +0200602#endif
603 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return NF_DROP;
606}
607
Simon Horman84b3cee2011-02-04 18:33:01 +0900608#ifdef CONFIG_SYSCTL
609
610static int sysctl_snat_reroute(struct sk_buff *skb)
611{
612 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
613 return ipvs->sysctl_snat_reroute;
614}
615
Simon Horman0cfa5582011-02-04 18:33:01 +0900616static int sysctl_nat_icmp_send(struct net *net)
617{
618 struct netns_ipvs *ipvs = net_ipvs(net);
619 return ipvs->sysctl_nat_icmp_send;
620}
621
Simon Horman71a8ab62011-02-04 18:33:01 +0900622static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
623{
624 return ipvs->sysctl_expire_nodest_conn;
625}
626
Simon Horman84b3cee2011-02-04 18:33:01 +0900627#else
628
629static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; }
Simon Horman0cfa5582011-02-04 18:33:01 +0900630static int sysctl_nat_icmp_send(struct net *net) { return 0; }
Simon Horman71a8ab62011-02-04 18:33:01 +0900631static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
Simon Horman84b3cee2011-02-04 18:33:01 +0900632
633#endif
634
Al Virob1550f22006-11-14 21:37:50 -0800635__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Al Virod3bc23e2006-11-14 21:24:49 -0800637 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300640static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
641{
642 if (NF_INET_LOCAL_IN == hooknum)
643 return IP_DEFRAG_VS_IN;
644 if (NF_INET_FORWARD == hooknum)
645 return IP_DEFRAG_VS_FWD;
646 return IP_DEFRAG_VS_OUT;
647}
648
Herbert Xu776c7292007-10-14 00:38:32 -0700649static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Julian Anastasovac692692013-03-22 11:46:54 +0200651 int err;
Herbert Xu776c7292007-10-14 00:38:32 -0700652
Julian Anastasovac692692013-03-22 11:46:54 +0200653 local_bh_disable();
654 err = ip_defrag(skb, user);
655 local_bh_enable();
Herbert Xu776c7292007-10-14 00:38:32 -0700656 if (!err)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700657 ip_send_check(ip_hdr(skb));
Herbert Xu776c7292007-10-14 00:38:32 -0700658
659 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Julian Anastasov579eb622014-12-18 22:41:23 +0200662static int ip_vs_route_me_harder(int af, struct sk_buff *skb,
663 unsigned int hooknum)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900664{
Julian Anastasov579eb622014-12-18 22:41:23 +0200665 if (!sysctl_snat_reroute(skb))
666 return 0;
667 /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
668 if (NF_INET_LOCAL_IN == hooknum)
669 return 0;
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900670#ifdef CONFIG_IP_VS_IPV6
671 if (af == AF_INET6) {
Julian Anastasov579eb622014-12-18 22:41:23 +0200672 struct dst_entry *dst = skb_dst(skb);
673
674 if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
675 ip6_route_me_harder(skb) != 0)
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900676 return 1;
677 } else
678#endif
Julian Anastasov579eb622014-12-18 22:41:23 +0200679 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900680 ip_route_me_harder(skb, RTN_LOCAL) != 0)
681 return 1;
682
683 return 0;
684}
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/*
687 * Packet has been made sufficiently writable in caller
688 * - inout: 1=in->out, 0=out->in
689 */
690void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
691 struct ip_vs_conn *cp, int inout)
692{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700693 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 unsigned int icmp_offset = iph->ihl*4;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700695 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
696 icmp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
698
699 if (inout) {
Julius Volze7ade462008-09-02 15:55:33 +0200700 iph->saddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200702 ciph->daddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 ip_send_check(ciph);
704 } else {
Julius Volze7ade462008-09-02 15:55:33 +0200705 iph->daddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200707 ciph->saddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 ip_send_check(ciph);
709 }
710
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100711 /* the TCP/UDP/SCTP port */
712 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
713 IPPROTO_SCTP == ciph->protocol) {
Al Viro014d7302006-09-28 14:29:52 -0700714 __be16 *ports = (void *)ciph + ciph->ihl*4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (inout)
717 ports[1] = cp->vport;
718 else
719 ports[0] = cp->dport;
720 }
721
722 /* And finally the ICMP checksum */
723 icmph->checksum = 0;
724 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
725 skb->ip_summed = CHECKSUM_UNNECESSARY;
726
727 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300728 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 "Forwarding altered outgoing ICMP");
730 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300731 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 "Forwarding altered incoming ICMP");
733}
734
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200735#ifdef CONFIG_IP_VS_IPV6
736void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
737 struct ip_vs_conn *cp, int inout)
738{
739 struct ipv6hdr *iph = ipv6_hdr(skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200740 unsigned int icmp_offset = 0;
741 unsigned int offs = 0; /* header offset*/
742 int protocol;
743 struct icmp6hdr *icmph;
744 struct ipv6hdr *ciph;
745 unsigned short fragoffs;
746
747 ipv6_find_hdr(skb, &icmp_offset, IPPROTO_ICMPV6, &fragoffs, NULL);
748 icmph = (struct icmp6hdr *)(skb_network_header(skb) + icmp_offset);
749 offs = icmp_offset + sizeof(struct icmp6hdr);
750 ciph = (struct ipv6hdr *)(skb_network_header(skb) + offs);
751
752 protocol = ipv6_find_hdr(skb, &offs, -1, &fragoffs, NULL);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200753
754 if (inout) {
755 iph->saddr = cp->vaddr.in6;
756 ciph->daddr = cp->vaddr.in6;
757 } else {
758 iph->daddr = cp->daddr.in6;
759 ciph->saddr = cp->daddr.in6;
760 }
761
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100762 /* the TCP/UDP/SCTP port */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200763 if (!fragoffs && (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
764 IPPROTO_SCTP == protocol)) {
765 __be16 *ports = (void *)(skb_network_header(skb) + offs);
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200766
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200767 IP_VS_DBG(11, "%s() changed port %d to %d\n", __func__,
768 ntohs(inout ? ports[1] : ports[0]),
769 ntohs(inout ? cp->vport : cp->dport));
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200770 if (inout)
771 ports[1] = cp->vport;
772 else
773 ports[0] = cp->dport;
774 }
775
776 /* And finally the ICMP checksum */
Simon Horman8870f842010-08-26 13:21:26 -0700777 icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
778 skb->len - icmp_offset,
779 IPPROTO_ICMPV6, 0);
780 skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
781 skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
782 skb->ip_summed = CHECKSUM_PARTIAL;
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200783
784 if (inout)
Julian Anastasov0d796412010-10-17 16:46:17 +0300785 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
786 (void *)ciph - (void *)iph,
787 "Forwarding altered outgoing ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200788 else
Julian Anastasov0d796412010-10-17 16:46:17 +0300789 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
790 (void *)ciph - (void *)iph,
791 "Forwarding altered incoming ICMPv6");
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200792}
793#endif
794
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000795/* Handle relevant response ICMP messages - forward to the right
Julian Anastasov6cb90db2011-02-09 02:26:38 +0200796 * destination host.
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000797 */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000798static int handle_response_icmp(int af, struct sk_buff *skb,
799 union nf_inet_addr *snet,
800 __u8 protocol, struct ip_vs_conn *cp,
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000801 struct ip_vs_protocol *pp,
Julian Anastasov579eb622014-12-18 22:41:23 +0200802 unsigned int offset, unsigned int ihl,
803 unsigned int hooknum)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000804{
805 unsigned int verdict = NF_DROP;
806
807 if (IP_VS_FWD_METHOD(cp) != 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +0000808 pr_err("shouldn't reach here, because the box is on the "
809 "half connection in the tun/dr module.\n");
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000810 }
811
812 /* Ensure the checksum is correct */
813 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
814 /* Failed checksum! */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000815 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
816 IP_VS_DBG_ADDR(af, snet));
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000817 goto out;
818 }
819
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100820 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
821 IPPROTO_SCTP == protocol)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000822 offset += 2 * sizeof(__u16);
823 if (!skb_make_writable(skb, offset))
824 goto out;
825
Simon Hormanf2428ed2008-09-05 11:17:14 +1000826#ifdef CONFIG_IP_VS_IPV6
827 if (af == AF_INET6)
828 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
829 else
830#endif
831 ip_vs_nat_icmp(skb, pp, cp, 1);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000832
Julian Anastasov579eb622014-12-18 22:41:23 +0200833 if (ip_vs_route_me_harder(af, skb, hooknum))
Simon Hormanba4fd7e2011-02-04 18:33:01 +0900834 goto out;
Julian Anastasovf5a41842010-10-17 16:35:46 +0300835
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000836 /* do the statistics and put it back */
837 ip_vs_out_stats(cp, skb);
838
Julian Anastasovcf356d62010-10-17 16:21:07 +0300839 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200840 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +0300841 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +0200842 else
843 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000844 verdict = NF_ACCEPT;
845
846out:
847 __ip_vs_conn_put(cp);
848
849 return verdict;
850}
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852/*
853 * Handle ICMP messages in the inside-to-outside direction (outgoing).
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000854 * Find any that might be relevant, check against existing connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 * Currently handles error types - unreachable, quench, ttl exceeded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300857static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
858 unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct iphdr *iph;
861 struct icmphdr _icmph, *ic;
862 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +0200863 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 struct ip_vs_conn *cp;
865 struct ip_vs_protocol *pp;
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000866 unsigned int offset, ihl;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000867 union nf_inet_addr snet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 *related = 1;
870
871 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -0700872 if (ip_is_fragment(ip_hdr(skb))) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300873 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700877 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 offset = ihl = iph->ihl * 4;
879 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
880 if (ic == NULL)
881 return NF_DROP;
882
Harvey Harrison14d5e832008-10-31 00:54:29 -0700883 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -0700885 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /*
888 * Work through seeing if this is for us.
889 * These checks are supposed to be in an order that means easy
890 * things are checked first to speed up processing.... however
891 * this means that some packets will manage to get a long way
892 * down this stack and then be rejected, but that's life.
893 */
894 if ((ic->type != ICMP_DEST_UNREACH) &&
895 (ic->type != ICMP_SOURCE_QUENCH) &&
896 (ic->type != ICMP_TIME_EXCEEDED)) {
897 *related = 0;
898 return NF_ACCEPT;
899 }
900
901 /* Now find the contained IP header */
902 offset += sizeof(_icmph);
903 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
904 if (cih == NULL)
905 return NF_ACCEPT; /* The packet looks wrong, ignore */
906
907 pp = ip_vs_proto_get(cih->protocol);
908 if (!pp)
909 return NF_ACCEPT;
910
911 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900912 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 pp->dont_defrag))
914 return NF_ACCEPT;
915
Julian Anastasov0d796412010-10-17 16:46:17 +0300916 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
917 "Checking outgoing ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200919 ip_vs_fill_ip4hdr(cih, &ciph);
920 ciph.len += offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 /* The embedded headers contain source and dest in reverse order */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200922 cp = pp->conn_out_get(AF_INET, skb, &ciph, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (!cp)
924 return NF_ACCEPT;
925
Simon Hormanf2428ed2008-09-05 11:17:14 +1000926 snet.ip = iph->saddr;
927 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
Julian Anastasov579eb622014-12-18 22:41:23 +0200928 pp, ciph.len, ihl, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Julius Volz2a3b7912008-09-02 15:55:47 +0200931#ifdef CONFIG_IP_VS_IPV6
Julian Anastasov1ca5bb52010-10-17 16:32:29 +0300932static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200933 unsigned int hooknum, struct ip_vs_iphdr *ipvsh)
Julius Volz2a3b7912008-09-02 15:55:47 +0200934{
Julius Volz2a3b7912008-09-02 15:55:47 +0200935 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200936 struct ipv6hdr _ip6h, *ip6h; /* The ip header contained within ICMP */
937 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +0200938 struct ip_vs_conn *cp;
939 struct ip_vs_protocol *pp;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000940 union nf_inet_addr snet;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200941 unsigned int writable;
942
Julius Volz2a3b7912008-09-02 15:55:47 +0200943 *related = 1;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200944 ic = frag_safe_skb_hp(skb, ipvsh->len, sizeof(_icmph), &_icmph, ipvsh);
Julius Volz2a3b7912008-09-02 15:55:47 +0200945 if (ic == NULL)
946 return NF_DROP;
947
Julius Volz2a3b7912008-09-02 15:55:47 +0200948 /*
949 * Work through seeing if this is for us.
950 * These checks are supposed to be in an order that means easy
951 * things are checked first to speed up processing.... however
952 * this means that some packets will manage to get a long way
953 * down this stack and then be rejected, but that's life.
954 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +0200955 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +0200956 *related = 0;
957 return NF_ACCEPT;
958 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200959 /* Fragment header that is before ICMP header tells us that:
960 * it's not an error message since they can't be fragmented.
961 */
David S. Millere7165032012-11-30 12:01:30 -0500962 if (ipvsh->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +0200963 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +0200964
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200965 IP_VS_DBG(8, "Outgoing ICMPv6 (%d,%d) %pI6c->%pI6c\n",
966 ic->icmp6_type, ntohs(icmpv6_id(ic)),
967 &ipvsh->saddr, &ipvsh->daddr);
Julius Volz2a3b7912008-09-02 15:55:47 +0200968
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200969 /* Now find the contained IP header */
970 ciph.len = ipvsh->len + sizeof(_icmph);
971 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
972 if (ip6h == NULL)
973 return NF_ACCEPT; /* The packet looks wrong, ignore */
974 ciph.saddr.in6 = ip6h->saddr; /* conn_out_get() handles reverse order */
975 ciph.daddr.in6 = ip6h->daddr;
976 /* skip possible IPv6 exthdrs of contained IPv6 packet */
977 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
978 if (ciph.protocol < 0)
979 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
980
981 pp = ip_vs_proto_get(ciph.protocol);
Julius Volz2a3b7912008-09-02 15:55:47 +0200982 if (!pp)
983 return NF_ACCEPT;
984
Julius Volz2a3b7912008-09-02 15:55:47 +0200985 /* The embedded headers contain source and dest in reverse order */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +0200986 cp = pp->conn_out_get(AF_INET6, skb, &ciph, 1);
Julius Volz2a3b7912008-09-02 15:55:47 +0200987 if (!cp)
988 return NF_ACCEPT;
989
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +0200990 snet.in6 = ciph.saddr.in6;
991 writable = ciph.len;
992 return handle_response_icmp(AF_INET6, skb, &snet, ciph.protocol, cp,
Julian Anastasov579eb622014-12-18 22:41:23 +0200993 pp, writable, sizeof(struct ipv6hdr),
994 hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +0200995}
996#endif
997
Venkata Mohan Reddy2906f662010-02-18 12:31:05 +0100998/*
999 * Check if sctp chunc is ABORT chunk
1000 */
1001static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
1002{
1003 sctp_chunkhdr_t *sch, schunk;
1004 sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
1005 sizeof(schunk), &schunk);
1006 if (sch == NULL)
1007 return 0;
1008 if (sch->type == SCTP_CID_ABORT)
1009 return 1;
1010 return 0;
1011}
1012
Julius Volz2a3b7912008-09-02 15:55:47 +02001013static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 struct tcphdr _tcph, *th;
1016
Julius Volz2a3b7912008-09-02 15:55:47 +02001017 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (th == NULL)
1019 return 0;
1020 return th->rst;
1021}
1022
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001023static inline bool is_new_conn(const struct sk_buff *skb,
1024 struct ip_vs_iphdr *iph)
1025{
1026 switch (iph->protocol) {
1027 case IPPROTO_TCP: {
1028 struct tcphdr _tcph, *th;
1029
1030 th = skb_header_pointer(skb, iph->len, sizeof(_tcph), &_tcph);
1031 if (th == NULL)
1032 return false;
1033 return th->syn;
1034 }
1035 case IPPROTO_SCTP: {
1036 sctp_chunkhdr_t *sch, schunk;
1037
1038 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
1039 sizeof(schunk), &schunk);
1040 if (sch == NULL)
1041 return false;
1042 return sch->type == SCTP_CID_INIT;
1043 }
1044 default:
1045 return false;
1046 }
1047}
1048
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001049/* Handle response packets: rewrite addresses and send away...
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001050 */
1051static unsigned int
Hans Schillstrom93304192011-01-03 14:44:51 +01001052handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
Julian Anastasov579eb622014-12-18 22:41:23 +02001053 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph,
1054 unsigned int hooknum)
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001055{
Hans Schillstrom93304192011-01-03 14:44:51 +01001056 struct ip_vs_protocol *pp = pd->pp;
1057
Julian Anastasov0d796412010-10-17 16:46:17 +03001058 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001059
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001060 if (!skb_make_writable(skb, iph->len))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001061 goto drop;
1062
1063 /* mangle the packet */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001064 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp, iph))
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001065 goto drop;
1066
1067#ifdef CONFIG_IP_VS_IPV6
1068 if (af == AF_INET6)
1069 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1070 else
1071#endif
1072 {
1073 ip_hdr(skb)->saddr = cp->vaddr.ip;
1074 ip_send_check(ip_hdr(skb));
1075 }
1076
Julian Anastasov8a803042010-09-21 17:38:57 +02001077 /*
1078 * nf_iterate does not expect change in the skb->dst->dev.
1079 * It looks like it is not fatal to enable this code for hooks
1080 * where our handlers are at the end of the chain list and
1081 * when all next handlers use skb->dst->dev and not outdev.
1082 * It will definitely route properly the inout NAT traffic
1083 * when multiple paths are used.
1084 */
1085
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001086 /* For policy routing, packets originating from this
1087 * machine itself may be routed differently to packets
1088 * passing through. We want this packet to be routed as
1089 * if it came from this machine itself. So re-compute
1090 * the routing information.
1091 */
Julian Anastasov579eb622014-12-18 22:41:23 +02001092 if (ip_vs_route_me_harder(af, skb, hooknum))
Simon Hormanba4fd7e2011-02-04 18:33:01 +09001093 goto drop;
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001094
Julian Anastasov0d796412010-10-17 16:46:17 +03001095 IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001096
1097 ip_vs_out_stats(cp, skb);
Hans Schillstrom93304192011-01-03 14:44:51 +01001098 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
Julian Anastasovcf356d62010-10-17 16:21:07 +03001099 skb->ipvs_property = 1;
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001100 if (!(cp->flags & IP_VS_CONN_F_NFCT))
Julian Anastasovcf356d62010-10-17 16:21:07 +03001101 ip_vs_notrack(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001102 else
1103 ip_vs_update_conntrack(skb, cp, 0);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001104 ip_vs_conn_put(cp);
1105
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001106 LeaveFunction(11);
1107 return NF_ACCEPT;
1108
1109drop:
1110 ip_vs_conn_put(cp);
1111 kfree_skb(skb);
Julian Anastasovf4bc17c2010-09-21 17:35:41 +02001112 LeaveFunction(11);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001113 return NF_STOLEN;
1114}
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/*
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001117 * Check if outgoing packet belongs to the established ip_vs_conn.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
1119static unsigned int
Julian Anastasovfc604762010-10-17 16:38:15 +03001120ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Hans Schillstromfc723252011-01-03 14:44:43 +01001122 struct net *net = NULL;
Julius Volz51ef3482008-09-02 15:55:40 +02001123 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001125 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct ip_vs_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 EnterFunction(11);
1129
Julian Anastasovfc604762010-10-17 16:38:15 +03001130 /* Already marked as IPVS request or reply? */
Harald Welte6869c4d2005-08-09 19:24:19 -07001131 if (skb->ipvs_property)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return NF_ACCEPT;
1133
Julian Anastasovfc604762010-10-17 16:38:15 +03001134 /* Bad... Do not break raw sockets */
1135 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1136 af == AF_INET)) {
1137 struct sock *sk = skb->sk;
1138 struct inet_sock *inet = inet_sk(skb->sk);
1139
1140 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1141 return NF_ACCEPT;
1142 }
1143
1144 if (unlikely(!skb_dst(skb)))
1145 return NF_ACCEPT;
1146
Hans Schillstromfc723252011-01-03 14:44:43 +01001147 net = skb_net(skb);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001148 if (!net_ipvs(net)->enable)
1149 return NF_ACCEPT;
1150
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001151 ip_vs_fill_iph_skb(af, skb, &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001152#ifdef CONFIG_IP_VS_IPV6
1153 if (af == AF_INET6) {
1154 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001155 int related;
1156 int verdict = ip_vs_out_icmp_v6(skb, &related,
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001157 hooknum, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Julian Anastasovf5a41842010-10-17 16:35:46 +03001159 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001160 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001161 }
1162 } else
1163#endif
1164 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001165 int related;
1166 int verdict = ip_vs_out_icmp(skb, &related, hooknum);
Julius Volz2a3b7912008-09-02 15:55:47 +02001167
Julian Anastasovf5a41842010-10-17 16:35:46 +03001168 if (related)
Julius Volz2a3b7912008-09-02 15:55:47 +02001169 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Hans Schillstrom93304192011-01-03 14:44:51 +01001172 pd = ip_vs_proto_data_get(net, iph.protocol);
1173 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001175 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 /* reassemble IP fragments */
Julius Volz2a3b7912008-09-02 15:55:47 +02001178#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001179 if (af == AF_INET)
Julius Volz2a3b7912008-09-02 15:55:47 +02001180#endif
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001181 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001182 if (ip_vs_gather_frags(skb,
1183 ip_vs_defrag_user(hooknum)))
Julius Volz2a3b7912008-09-02 15:55:47 +02001184 return NF_STOLEN;
1185
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001186 ip_vs_fill_ip4hdr(skb_network_header(skb), &iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 /*
1190 * Check if the packet belongs to an existing entry
1191 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001192 cp = pp->conn_out_get(af, skb, &iph, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Julian Anastasovcb591552010-10-17 16:40:51 +03001194 if (likely(cp))
Julian Anastasov579eb622014-12-18 22:41:23 +02001195 return handle_response(af, skb, pd, cp, &iph, hooknum);
Simon Horman0cfa5582011-02-04 18:33:01 +09001196 if (sysctl_nat_icmp_send(net) &&
Julian Anastasovcb591552010-10-17 16:40:51 +03001197 (pp->protocol == IPPROTO_TCP ||
1198 pp->protocol == IPPROTO_UDP ||
1199 pp->protocol == IPPROTO_SCTP)) {
1200 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001202 pptr = frag_safe_skb_hp(skb, iph.len,
1203 sizeof(_ports), _ports, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001204 if (pptr == NULL)
1205 return NF_ACCEPT; /* Not for me */
Julian Anastasov276472e2013-03-21 11:58:08 +02001206 if (ip_vs_has_real_service(net, af, iph.protocol, &iph.saddr,
1207 pptr[0])) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001208 /*
1209 * Notify the real server: there is no
1210 * existing entry if it is not RST
1211 * packet or not TCP packet.
1212 */
1213 if ((iph.protocol != IPPROTO_TCP &&
1214 iph.protocol != IPPROTO_SCTP)
1215 || ((iph.protocol == IPPROTO_TCP
1216 && !is_tcp_reset(skb, iph.len))
1217 || (iph.protocol == IPPROTO_SCTP
1218 && !is_sctp_abort(skb,
1219 iph.len)))) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001220#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001221 if (af == AF_INET6) {
Julian Anastasovcb591552010-10-17 16:40:51 +03001222 if (!skb->dev)
1223 skb->dev = net->loopback_dev;
1224 icmpv6_send(skb,
1225 ICMPV6_DEST_UNREACH,
1226 ICMPV6_PORT_UNREACH,
1227 0);
1228 } else
Julius Volz2a3b7912008-09-02 15:55:47 +02001229#endif
Julian Anastasovcb591552010-10-17 16:40:51 +03001230 icmp_send(skb,
1231 ICMP_DEST_UNREACH,
1232 ICMP_PORT_UNREACH, 0);
1233 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
Julian Anastasov0d796412010-10-17 16:46:17 +03001237 IP_VS_DBG_PKT(12, af, pp, skb, 0,
Julian Anastasovcb591552010-10-17 16:40:51 +03001238 "ip_vs_out: packet continues traversal as normal");
1239 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Julian Anastasovfc604762010-10-17 16:38:15 +03001242/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001243 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1244 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001245 * Check if packet is reply for established ip_vs_conn.
1246 */
1247static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001248ip_vs_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovfc604762010-10-17 16:38:15 +03001249 const struct net_device *in, const struct net_device *out,
1250 int (*okfn)(struct sk_buff *))
1251{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001252 return ip_vs_out(ops->hooknum, skb, AF_INET);
Julian Anastasovfc604762010-10-17 16:38:15 +03001253}
1254
1255/*
1256 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1257 * Check if packet is reply for established ip_vs_conn.
1258 */
1259static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001260ip_vs_local_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovfc604762010-10-17 16:38:15 +03001261 const struct net_device *in, const struct net_device *out,
1262 int (*okfn)(struct sk_buff *))
1263{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001264 return ip_vs_out(ops->hooknum, skb, AF_INET);
Julian Anastasovfc604762010-10-17 16:38:15 +03001265}
1266
1267#ifdef CONFIG_IP_VS_IPV6
1268
1269/*
Julian Anastasovcb591552010-10-17 16:40:51 +03001270 * It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1271 * used only for VS/NAT.
Julian Anastasovfc604762010-10-17 16:38:15 +03001272 * Check if packet is reply for established ip_vs_conn.
1273 */
1274static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001275ip_vs_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovfc604762010-10-17 16:38:15 +03001276 const struct net_device *in, const struct net_device *out,
1277 int (*okfn)(struct sk_buff *))
1278{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001279 return ip_vs_out(ops->hooknum, skb, AF_INET6);
Julian Anastasovfc604762010-10-17 16:38:15 +03001280}
1281
1282/*
1283 * It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1284 * Check if packet is reply for established ip_vs_conn.
1285 */
1286static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001287ip_vs_local_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovfc604762010-10-17 16:38:15 +03001288 const struct net_device *in, const struct net_device *out,
1289 int (*okfn)(struct sk_buff *))
1290{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001291 return ip_vs_out(ops->hooknum, skb, AF_INET6);
Julian Anastasovfc604762010-10-17 16:38:15 +03001292}
1293
1294#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296/*
1297 * Handle ICMP messages in the outside-to-inside direction (incoming).
1298 * Find any that might be relevant, check against existing connections,
1299 * forward to the right destination host if relevant.
1300 * Currently handles error types - unreachable, quench, ttl exceeded.
1301 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001302static int
Herbert Xu3db05fe2007-10-15 00:53:15 -07001303ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Hans Schillstrom93304192011-01-03 14:44:51 +01001305 struct net *net = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 struct iphdr *iph;
1307 struct icmphdr _icmph, *ic;
1308 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +02001309 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 struct ip_vs_conn *cp;
1311 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001312 struct ip_vs_proto_data *pd;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001313 unsigned int offset, offset2, ihl, verdict;
1314 bool ipip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 *related = 1;
1317
1318 /* reassemble IP fragments */
Paul Gortmaker56f8a752011-06-21 20:33:34 -07001319 if (ip_is_fragment(ip_hdr(skb))) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001320 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001324 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 offset = ihl = iph->ihl * 4;
1326 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1327 if (ic == NULL)
1328 return NF_DROP;
1329
Harvey Harrison14d5e832008-10-31 00:54:29 -07001330 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -07001332 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /*
1335 * Work through seeing if this is for us.
1336 * These checks are supposed to be in an order that means easy
1337 * things are checked first to speed up processing.... however
1338 * this means that some packets will manage to get a long way
1339 * down this stack and then be rejected, but that's life.
1340 */
1341 if ((ic->type != ICMP_DEST_UNREACH) &&
1342 (ic->type != ICMP_SOURCE_QUENCH) &&
1343 (ic->type != ICMP_TIME_EXCEEDED)) {
1344 *related = 0;
1345 return NF_ACCEPT;
1346 }
1347
1348 /* Now find the contained IP header */
1349 offset += sizeof(_icmph);
1350 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1351 if (cih == NULL)
1352 return NF_ACCEPT; /* The packet looks wrong, ignore */
1353
Hans Schillstrom93304192011-01-03 14:44:51 +01001354 net = skb_net(skb);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001355
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001356 /* Special case for errors for IPIP packets */
1357 ipip = false;
1358 if (cih->protocol == IPPROTO_IPIP) {
1359 if (unlikely(cih->frag_off & htons(IP_OFFSET)))
1360 return NF_ACCEPT;
1361 /* Error for our IPIP must arrive at LOCAL_IN */
1362 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL))
1363 return NF_ACCEPT;
1364 offset += cih->ihl * 4;
1365 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1366 if (cih == NULL)
1367 return NF_ACCEPT; /* The packet looks wrong, ignore */
1368 ipip = true;
1369 }
1370
Hans Schillstrom93304192011-01-03 14:44:51 +01001371 pd = ip_vs_proto_data_get(net, cih->protocol);
1372 if (!pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001374 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +09001377 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 pp->dont_defrag))
1379 return NF_ACCEPT;
1380
Julian Anastasov0d796412010-10-17 16:46:17 +03001381 IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1382 "Checking incoming ICMP for");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001384 offset2 = offset;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001385 ip_vs_fill_ip4hdr(cih, &ciph);
1386 ciph.len += offset;
1387 offset = ciph.len;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001388 /* The embedded headers contain source and dest in reverse order.
1389 * For IPIP this is error for request, not for reply.
1390 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001391 cp = pp->conn_in_get(AF_INET, skb, &ciph, ipip ? 0 : 1);
Julian Anastasov6cb90db2011-02-09 02:26:38 +02001392 if (!cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return NF_ACCEPT;
1394
1395 verdict = NF_DROP;
1396
1397 /* Ensure the checksum is correct */
Herbert Xu60476372007-04-09 11:59:39 -07001398 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* Failed checksum! */
Harvey Harrison14d5e832008-10-31 00:54:29 -07001400 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1401 &iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 goto out;
1403 }
1404
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001405 if (ipip) {
1406 __be32 info = ic->un.gateway;
Peter Christensenf44a5f42014-05-24 21:40:12 +02001407 __u8 type = ic->type;
1408 __u8 code = ic->code;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001409
1410 /* Update the MTU */
1411 if (ic->type == ICMP_DEST_UNREACH &&
1412 ic->code == ICMP_FRAG_NEEDED) {
1413 struct ip_vs_dest *dest = cp->dest;
1414 u32 mtu = ntohs(ic->un.frag.mtu);
Peter Christensenf44a5f42014-05-24 21:40:12 +02001415 __be16 frag_off = cih->frag_off;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001416
1417 /* Strip outer IP and ICMP, go to IPIP header */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001418 if (pskb_pull(skb, ihl + sizeof(_icmph)) == NULL)
1419 goto ignore_ipip;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001420 offset2 -= ihl + sizeof(_icmph);
1421 skb_reset_network_header(skb);
1422 IP_VS_DBG(12, "ICMP for IPIP %pI4->%pI4: mtu=%u\n",
1423 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr, mtu);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001424 ipv4_update_pmtu(skb, dev_net(skb->dev),
1425 mtu, 0, 0, 0, 0);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001426 /* Client uses PMTUD? */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001427 if (!(frag_off & htons(IP_DF)))
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001428 goto ignore_ipip;
1429 /* Prefer the resulting PMTU */
1430 if (dest) {
Julian Anastasov026ace02013-03-21 11:58:06 +02001431 struct ip_vs_dest_dst *dest_dst;
1432
1433 rcu_read_lock();
1434 dest_dst = rcu_dereference(dest->dest_dst);
1435 if (dest_dst)
1436 mtu = dst_mtu(dest_dst->dst_cache);
1437 rcu_read_unlock();
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001438 }
1439 if (mtu > 68 + sizeof(struct iphdr))
1440 mtu -= sizeof(struct iphdr);
1441 info = htonl(mtu);
1442 }
1443 /* Strip outer IP, ICMP and IPIP, go to IP header of
1444 * original request.
1445 */
Peter Christensenf44a5f42014-05-24 21:40:12 +02001446 if (pskb_pull(skb, offset2) == NULL)
1447 goto ignore_ipip;
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001448 skb_reset_network_header(skb);
1449 IP_VS_DBG(12, "Sending ICMP for %pI4->%pI4: t=%u, c=%u, i=%u\n",
1450 &ip_hdr(skb)->saddr, &ip_hdr(skb)->daddr,
Peter Christensenf44a5f42014-05-24 21:40:12 +02001451 type, code, ntohl(info));
1452 icmp_send(skb, type, code, info);
Julian Anastasovf2edb9f2012-07-20 11:59:52 +03001453 /* ICMP can be shorter but anyways, account it */
1454 ip_vs_out_stats(cp, skb);
1455
1456ignore_ipip:
1457 consume_skb(skb);
1458 verdict = NF_STOLEN;
1459 goto out;
1460 }
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* do the statistics and put it back */
1463 ip_vs_in_stats(cp, skb);
Julian Anastasov06f3d7f2013-06-18 10:08:06 +03001464 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol ||
1465 IPPROTO_SCTP == cih->protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 offset += 2 * sizeof(__u16);
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001467 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Hans Schillstrom552ad652011-06-13 12:19:26 +02001469out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 __ip_vs_conn_put(cp);
1471
1472 return verdict;
1473}
1474
Julius Volz2a3b7912008-09-02 15:55:47 +02001475#ifdef CONFIG_IP_VS_IPV6
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001476static int ip_vs_in_icmp_v6(struct sk_buff *skb, int *related,
1477 unsigned int hooknum, struct ip_vs_iphdr *iph)
Julius Volz2a3b7912008-09-02 15:55:47 +02001478{
Hans Schillstrom93304192011-01-03 14:44:51 +01001479 struct net *net = NULL;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001480 struct ipv6hdr _ip6h, *ip6h;
Julius Volz2a3b7912008-09-02 15:55:47 +02001481 struct icmp6hdr _icmph, *ic;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001482 struct ip_vs_iphdr ciph = {.flags = 0, .fragoffs = 0};/*Contained IP */
Julius Volz2a3b7912008-09-02 15:55:47 +02001483 struct ip_vs_conn *cp;
1484 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001485 struct ip_vs_proto_data *pd;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001486 unsigned int offs_ciph, writable, verdict;
1487
Julius Volz2a3b7912008-09-02 15:55:47 +02001488 *related = 1;
1489
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001490 ic = frag_safe_skb_hp(skb, iph->len, sizeof(_icmph), &_icmph, iph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001491 if (ic == NULL)
1492 return NF_DROP;
1493
Julius Volz2a3b7912008-09-02 15:55:47 +02001494 /*
1495 * Work through seeing if this is for us.
1496 * These checks are supposed to be in an order that means easy
1497 * things are checked first to speed up processing.... however
1498 * this means that some packets will manage to get a long way
1499 * down this stack and then be rejected, but that's life.
1500 */
Jesper Dangaard Brouer2fab8912012-09-26 14:06:11 +02001501 if (ic->icmp6_type & ICMPV6_INFOMSG_MASK) {
Julius Volz2a3b7912008-09-02 15:55:47 +02001502 *related = 0;
1503 return NF_ACCEPT;
1504 }
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001505 /* Fragment header that is before ICMP header tells us that:
1506 * it's not an error message since they can't be fragmented.
1507 */
David S. Millere7165032012-11-30 12:01:30 -05001508 if (iph->flags & IP6_FH_F_FRAG)
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001509 return NF_DROP;
Julius Volz2a3b7912008-09-02 15:55:47 +02001510
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001511 IP_VS_DBG(8, "Incoming ICMPv6 (%d,%d) %pI6c->%pI6c\n",
1512 ic->icmp6_type, ntohs(icmpv6_id(ic)),
1513 &iph->saddr, &iph->daddr);
1514
Julius Volz2a3b7912008-09-02 15:55:47 +02001515 /* Now find the contained IP header */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001516 ciph.len = iph->len + sizeof(_icmph);
1517 offs_ciph = ciph.len; /* Save ip header offset */
1518 ip6h = skb_header_pointer(skb, ciph.len, sizeof(_ip6h), &_ip6h);
1519 if (ip6h == NULL)
Julius Volz2a3b7912008-09-02 15:55:47 +02001520 return NF_ACCEPT; /* The packet looks wrong, ignore */
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001521 ciph.saddr.in6 = ip6h->saddr; /* conn_in_get() handles reverse order */
1522 ciph.daddr.in6 = ip6h->daddr;
1523 /* skip possible IPv6 exthdrs of contained IPv6 packet */
1524 ciph.protocol = ipv6_find_hdr(skb, &ciph.len, -1, &ciph.fragoffs, NULL);
1525 if (ciph.protocol < 0)
1526 return NF_ACCEPT; /* Contained IPv6 hdr looks wrong, ignore */
Julius Volz2a3b7912008-09-02 15:55:47 +02001527
Hans Schillstrom93304192011-01-03 14:44:51 +01001528 net = skb_net(skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001529 pd = ip_vs_proto_data_get(net, ciph.protocol);
Hans Schillstrom93304192011-01-03 14:44:51 +01001530 if (!pd)
Julius Volz2a3b7912008-09-02 15:55:47 +02001531 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001532 pp = pd->pp;
Julius Volz2a3b7912008-09-02 15:55:47 +02001533
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001534 /* Cannot handle fragmented embedded protocol */
1535 if (ciph.fragoffs)
Julius Volz2a3b7912008-09-02 15:55:47 +02001536 return NF_ACCEPT;
1537
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001538 IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offs_ciph,
Julian Anastasov0d796412010-10-17 16:46:17 +03001539 "Checking incoming ICMPv6 for");
Julius Volz2a3b7912008-09-02 15:55:47 +02001540
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001541 /* The embedded headers contain source and dest in reverse order
1542 * if not from localhost
1543 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001544 cp = pp->conn_in_get(AF_INET6, skb, &ciph,
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001545 (hooknum == NF_INET_LOCAL_OUT) ? 0 : 1);
1546
Julian Anastasov6cb90db2011-02-09 02:26:38 +02001547 if (!cp)
Julius Volz2a3b7912008-09-02 15:55:47 +02001548 return NF_ACCEPT;
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001549 /* VS/TUN, VS/DR and LOCALNODE just let it go */
1550 if ((hooknum == NF_INET_LOCAL_OUT) &&
1551 (IP_VS_FWD_METHOD(cp) != IP_VS_CONN_F_MASQ)) {
1552 __ip_vs_conn_put(cp);
1553 return NF_ACCEPT;
1554 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001555
Julius Volz2a3b7912008-09-02 15:55:47 +02001556 /* do the statistics and put it back */
1557 ip_vs_in_stats(cp, skb);
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001558
1559 /* Need to mangle contained IPv6 header in ICMPv6 packet */
1560 writable = ciph.len;
1561 if (IPPROTO_TCP == ciph.protocol || IPPROTO_UDP == ciph.protocol ||
1562 IPPROTO_SCTP == ciph.protocol)
1563 writable += 2 * sizeof(__u16); /* Also mangle ports */
1564
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001565 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, writable, hooknum, &ciph);
Julius Volz2a3b7912008-09-02 15:55:47 +02001566
1567 __ip_vs_conn_put(cp);
1568
1569 return verdict;
1570}
1571#endif
1572
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574/*
1575 * Check if it's for virtual services, look it up,
1576 * and send it on its way...
1577 */
1578static unsigned int
Julian Anastasovcb591552010-10-17 16:40:51 +03001579ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Hans Schillstromf1313152011-01-03 14:44:55 +01001581 struct net *net;
Julius Volz51ef3482008-09-02 15:55:40 +02001582 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 struct ip_vs_protocol *pp;
Hans Schillstrom93304192011-01-03 14:44:51 +01001584 struct ip_vs_proto_data *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 struct ip_vs_conn *cp;
Simon Horman4a516f12011-09-16 14:11:49 +09001586 int ret, pkts;
Hans Schillstromf1313152011-01-03 14:44:55 +01001587 struct netns_ipvs *ipvs;
Julius Volz51ef3482008-09-02 15:55:40 +02001588
Julian Anastasovfc604762010-10-17 16:38:15 +03001589 /* Already marked as IPVS request or reply? */
1590 if (skb->ipvs_property)
1591 return NF_ACCEPT;
1592
Julian Anastasovcb591552010-10-17 16:40:51 +03001593 /*
1594 * Big tappo:
1595 * - remote client: only PACKET_HOST
1596 * - route: used for struct net when skb->dev is unset
1597 */
1598 if (unlikely((skb->pkt_type != PACKET_HOST &&
1599 hooknum != NF_INET_LOCAL_OUT) ||
1600 !skb_dst(skb))) {
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001601 ip_vs_fill_iph_skb(af, skb, &iph);
Julian Anastasovcb591552010-10-17 16:40:51 +03001602 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1603 " ignored in hook %u\n",
1604 skb->pkt_type, iph.protocol,
1605 IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1606 return NF_ACCEPT;
1607 }
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001608 /* ipvs enabled in this netns ? */
1609 net = skb_net(skb);
Julian Anastasov0c125822013-03-09 23:25:04 +02001610 ipvs = net_ipvs(net);
1611 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001612 return NF_ACCEPT;
1613
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001614 ip_vs_fill_iph_skb(af, skb, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Julian Anastasovcb591552010-10-17 16:40:51 +03001616 /* Bad... Do not break raw sockets */
1617 if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1618 af == AF_INET)) {
1619 struct sock *sk = skb->sk;
1620 struct inet_sock *inet = inet_sk(skb->sk);
1621
1622 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1623 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625
Julius Volz94b26552009-08-31 16:22:23 +02001626#ifdef CONFIG_IP_VS_IPV6
1627 if (af == AF_INET6) {
1628 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001629 int related;
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001630 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum,
1631 &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Julius Volz94b26552009-08-31 16:22:23 +02001633 if (related)
1634 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001635 }
1636 } else
1637#endif
1638 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Julian Anastasov1ca5bb52010-10-17 16:32:29 +03001639 int related;
1640 int verdict = ip_vs_in_icmp(skb, &related, hooknum);
Julius Volz94b26552009-08-31 16:22:23 +02001641
1642 if (related)
1643 return verdict;
Julius Volz94b26552009-08-31 16:22:23 +02001644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 /* Protocol supported? */
Hans Schillstrom93304192011-01-03 14:44:51 +01001647 pd = ip_vs_proto_data_get(net, iph.protocol);
1648 if (unlikely(!pd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return NF_ACCEPT;
Hans Schillstrom93304192011-01-03 14:44:51 +01001650 pp = pd->pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 /*
1652 * Check if the packet belongs to an existing connection entry
1653 */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001654 cp = pp->conn_in_get(af, skb, &iph, 0);
Grzegorz Lyczbadc7b3eb2013-05-13 23:56:24 +02001655
1656 if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp && cp->dest &&
1657 unlikely(!atomic_read(&cp->dest->weight)) && !iph.fragoffs &&
1658 is_new_conn(skb, &iph)) {
1659 ip_vs_conn_expire_now(cp);
1660 __ip_vs_conn_put(cp);
1661 cp = NULL;
1662 }
1663
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001664 if (unlikely(!cp) && !iph.fragoffs) {
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001665 /* No (second) fragments need to enter here, as nf_defrag_ipv6
1666 * replayed fragment zero will already have created the cp
1667 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 int v;
1669
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001670 /* Schedule and create new connection entry into &cp */
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001671 if (!pp->conn_schedule(af, skb, pd, &v, &cp, &iph))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return v;
1673 }
1674
1675 if (unlikely(!cp)) {
1676 /* sorry, all this trouble for a no-hit :) */
Julian Anastasov0d796412010-10-17 16:46:17 +03001677 IP_VS_DBG_PKT(12, af, pp, skb, 0,
Julian Anastasovcb591552010-10-17 16:40:51 +03001678 "ip_vs_in: packet continues traversal as normal");
Jiri Pirko6aafeef2013-11-06 17:52:20 +01001679 if (iph.fragoffs) {
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001680 /* Fragment that couldn't be mapped to a conn entry
Jesper Dangaard Brouer2f747132012-09-26 14:06:59 +02001681 * is missing module nf_defrag_ipv6
1682 */
1683 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
1684 IP_VS_DBG_PKT(7, af, pp, skb, 0, "unhandled fragment");
1685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return NF_ACCEPT;
1687 }
1688
Julian Anastasov0d796412010-10-17 16:46:17 +03001689 IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 /* Check the server status */
1691 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1692 /* the destination server is not available */
1693
Simon Horman71a8ab62011-02-04 18:33:01 +09001694 if (sysctl_expire_nodest_conn(ipvs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /* try to expire the connection immediately */
1696 ip_vs_conn_expire_now(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
Julian Anastasovdc8103f2005-11-08 09:40:05 -08001698 /* don't restart its timer, and silently
1699 drop the packet. */
1700 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return NF_DROP;
1702 }
1703
1704 ip_vs_in_stats(cp, skb);
Simon Horman4a516f12011-09-16 14:11:49 +09001705 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (cp->packet_xmit)
Jesper Dangaard Brouerd4383f02012-09-26 14:07:17 +02001707 ret = cp->packet_xmit(skb, cp, pp, &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 /* do not touch skb anymore */
1709 else {
1710 IP_VS_DBG_RL("warning: packet_xmit is null");
1711 ret = NF_ACCEPT;
1712 }
1713
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001714 /* Increase its packet counter and check if it is needed
1715 * to be synchronized
1716 *
1717 * Sync connection if it is about to close to
1718 * encorage the standby servers to update the connections timeout
Hans Schillstrom986a0752010-11-19 14:25:13 +01001719 *
1720 * For ONE_PKT let ip_vs_sync_conn() do the filter work.
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001721 */
Hans Schillstromf1313152011-01-03 14:44:55 +01001722
Hans Schillstrom986a0752010-11-19 14:25:13 +01001723 if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
Simon Horman59e03502011-02-04 18:33:01 +09001724 pkts = sysctl_sync_threshold(ipvs);
Hans Schillstrom986a0752010-11-19 14:25:13 +01001725 else
1726 pkts = atomic_add_return(1, &cp->in_pkts);
1727
Julian Anastasov749c42b2012-04-24 23:46:40 +03001728 if (ipvs->sync_state & IP_VS_STATE_MASTER)
1729 ip_vs_sync_conn(net, cp, pkts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 ip_vs_conn_put(cp);
1732 return ret;
1733}
1734
Julian Anastasovcb591552010-10-17 16:40:51 +03001735/*
1736 * AF_INET handler in NF_INET_LOCAL_IN chain
1737 * Schedule and forward packets from remote clients
1738 */
1739static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001740ip_vs_remote_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovcb591552010-10-17 16:40:51 +03001741 const struct net_device *in,
1742 const struct net_device *out,
1743 int (*okfn)(struct sk_buff *))
1744{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001745 return ip_vs_in(ops->hooknum, skb, AF_INET);
Julian Anastasovcb591552010-10-17 16:40:51 +03001746}
1747
1748/*
1749 * AF_INET handler in NF_INET_LOCAL_OUT chain
1750 * Schedule and forward packets from local clients
1751 */
1752static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001753ip_vs_local_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovcb591552010-10-17 16:40:51 +03001754 const struct net_device *in, const struct net_device *out,
1755 int (*okfn)(struct sk_buff *))
1756{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001757 return ip_vs_in(ops->hooknum, skb, AF_INET);
Julian Anastasovcb591552010-10-17 16:40:51 +03001758}
1759
1760#ifdef CONFIG_IP_VS_IPV6
1761
1762/*
1763 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1764 * Schedule and forward packets from remote clients
1765 */
1766static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001767ip_vs_remote_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovcb591552010-10-17 16:40:51 +03001768 const struct net_device *in,
1769 const struct net_device *out,
1770 int (*okfn)(struct sk_buff *))
1771{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001772 return ip_vs_in(ops->hooknum, skb, AF_INET6);
Julian Anastasovcb591552010-10-17 16:40:51 +03001773}
1774
1775/*
1776 * AF_INET6 handler in NF_INET_LOCAL_OUT chain
1777 * Schedule and forward packets from local clients
1778 */
1779static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001780ip_vs_local_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julian Anastasovcb591552010-10-17 16:40:51 +03001781 const struct net_device *in, const struct net_device *out,
1782 int (*okfn)(struct sk_buff *))
1783{
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001784 return ip_vs_in(ops->hooknum, skb, AF_INET6);
Julian Anastasovcb591552010-10-17 16:40:51 +03001785}
1786
1787#endif
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790/*
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001791 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 * related packets destined for 0.0.0.0/0.
1793 * When fwmark-based virtual service is used, such as transparent
1794 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1795 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001796 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 * and send them to ip_vs_in_icmp.
1798 */
1799static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001800ip_vs_forward_icmp(const struct nf_hook_ops *ops, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 const struct net_device *in, const struct net_device *out,
1802 int (*okfn)(struct sk_buff *))
1803{
1804 int r;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001805 struct net *net;
Julian Anastasov0c125822013-03-09 23:25:04 +02001806 struct netns_ipvs *ipvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Herbert Xu3db05fe2007-10-15 00:53:15 -07001808 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return NF_ACCEPT;
1810
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001811 /* ipvs enabled in this netns ? */
1812 net = skb_net(skb);
Julian Anastasov0c125822013-03-09 23:25:04 +02001813 ipvs = net_ipvs(net);
1814 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001815 return NF_ACCEPT;
1816
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001817 return ip_vs_in_icmp(skb, &r, ops->hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Julius Volz2a3b7912008-09-02 15:55:47 +02001820#ifdef CONFIG_IP_VS_IPV6
1821static unsigned int
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001822ip_vs_forward_icmp_v6(const struct nf_hook_ops *ops, struct sk_buff *skb,
Julius Volz2a3b7912008-09-02 15:55:47 +02001823 const struct net_device *in, const struct net_device *out,
1824 int (*okfn)(struct sk_buff *))
1825{
1826 int r;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001827 struct net *net;
Julian Anastasov0c125822013-03-09 23:25:04 +02001828 struct netns_ipvs *ipvs;
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001829 struct ip_vs_iphdr iphdr;
Julius Volz2a3b7912008-09-02 15:55:47 +02001830
Jesper Dangaard Brouer63dca2c2012-09-26 14:06:41 +02001831 ip_vs_fill_iph_skb(AF_INET6, skb, &iphdr);
1832 if (iphdr.protocol != IPPROTO_ICMPV6)
Julius Volz2a3b7912008-09-02 15:55:47 +02001833 return NF_ACCEPT;
1834
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001835 /* ipvs enabled in this netns ? */
1836 net = skb_net(skb);
Julian Anastasov0c125822013-03-09 23:25:04 +02001837 ipvs = net_ipvs(net);
1838 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001839 return NF_ACCEPT;
1840
Patrick McHardy795aa6e2013-10-10 09:21:55 +02001841 return ip_vs_in_icmp_v6(skb, &r, ops->hooknum, &iphdr);
Julius Volz2a3b7912008-09-02 15:55:47 +02001842}
1843#endif
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Patrick McHardy19994142007-12-05 01:23:00 -08001846static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
Julian Anastasovcb591552010-10-17 16:40:51 +03001847 /* After packet filtering, change source only for VS/NAT */
1848 {
1849 .hook = ip_vs_reply4,
1850 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001851 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001852 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001853 .priority = NF_IP_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001854 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001855 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1856 * or VS/NAT(change destination), so that filtering rules can be
1857 * applied to IPVS. */
1858 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001859 .hook = ip_vs_remote_request4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001860 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001861 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001862 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001863 .priority = NF_IP_PRI_NAT_SRC - 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001864 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001865 /* Before ip_vs_in, change source only for VS/NAT */
Patrick McHardy41c5b312007-12-05 01:22:43 -08001866 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001867 .hook = ip_vs_local_reply4,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001868 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001869 .pf = NFPROTO_IPV4,
Julian Anastasovfc604762010-10-17 16:38:15 +03001870 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001871 .priority = NF_IP_PRI_NAT_DST + 1,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001872 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001873 /* After mangle, schedule and forward local requests */
1874 {
1875 .hook = ip_vs_local_request4,
1876 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001877 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001878 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001879 .priority = NF_IP_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001880 },
Patrick McHardy41c5b312007-12-05 01:22:43 -08001881 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1882 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1883 {
1884 .hook = ip_vs_forward_icmp,
1885 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001886 .pf = NFPROTO_IPV4,
Julian Anastasovcb591552010-10-17 16:40:51 +03001887 .hooknum = NF_INET_FORWARD,
1888 .priority = 99,
Patrick McHardy41c5b312007-12-05 01:22:43 -08001889 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001890 /* After packet filtering, change source only for VS/NAT */
1891 {
1892 .hook = ip_vs_reply4,
1893 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001894 .pf = NFPROTO_IPV4,
Julian Anastasovfc604762010-10-17 16:38:15 +03001895 .hooknum = NF_INET_FORWARD,
1896 .priority = 100,
1897 },
Julius Volz473b23d2008-09-02 15:55:54 +02001898#ifdef CONFIG_IP_VS_IPV6
Julian Anastasovcb591552010-10-17 16:40:51 +03001899 /* After packet filtering, change source only for VS/NAT */
1900 {
1901 .hook = ip_vs_reply6,
1902 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001903 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001904 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001905 .priority = NF_IP6_PRI_NAT_SRC - 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001906 },
Julius Volz473b23d2008-09-02 15:55:54 +02001907 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1908 * or VS/NAT(change destination), so that filtering rules can be
1909 * applied to IPVS. */
1910 {
Julian Anastasovcb591552010-10-17 16:40:51 +03001911 .hook = ip_vs_remote_request6,
Julius Volz473b23d2008-09-02 15:55:54 +02001912 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001913 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001914 .hooknum = NF_INET_LOCAL_IN,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001915 .priority = NF_IP6_PRI_NAT_SRC - 1,
Julius Volz473b23d2008-09-02 15:55:54 +02001916 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001917 /* Before ip_vs_in, change source only for VS/NAT */
Julius Volz473b23d2008-09-02 15:55:54 +02001918 {
Julian Anastasovfc604762010-10-17 16:38:15 +03001919 .hook = ip_vs_local_reply6,
Julius Volz473b23d2008-09-02 15:55:54 +02001920 .owner = THIS_MODULE,
Julian Anastasoveb90b0c2014-08-22 17:53:41 +03001921 .pf = NFPROTO_IPV6,
Julian Anastasovfc604762010-10-17 16:38:15 +03001922 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001923 .priority = NF_IP6_PRI_NAT_DST + 1,
Julius Volz473b23d2008-09-02 15:55:54 +02001924 },
Julian Anastasovcb591552010-10-17 16:40:51 +03001925 /* After mangle, schedule and forward local requests */
1926 {
1927 .hook = ip_vs_local_request6,
1928 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001929 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001930 .hooknum = NF_INET_LOCAL_OUT,
Julian Anastasovafb523c2011-06-02 09:09:54 +09001931 .priority = NF_IP6_PRI_NAT_DST + 2,
Julian Anastasovcb591552010-10-17 16:40:51 +03001932 },
Julius Volz473b23d2008-09-02 15:55:54 +02001933 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1934 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1935 {
1936 .hook = ip_vs_forward_icmp_v6,
1937 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001938 .pf = NFPROTO_IPV6,
Julian Anastasovcb591552010-10-17 16:40:51 +03001939 .hooknum = NF_INET_FORWARD,
1940 .priority = 99,
Julius Volz473b23d2008-09-02 15:55:54 +02001941 },
Julian Anastasovfc604762010-10-17 16:38:15 +03001942 /* After packet filtering, change source only for VS/NAT */
1943 {
1944 .hook = ip_vs_reply6,
1945 .owner = THIS_MODULE,
Alban Crequy4c809d62012-05-14 03:56:38 +00001946 .pf = NFPROTO_IPV6,
Julian Anastasovfc604762010-10-17 16:38:15 +03001947 .hooknum = NF_INET_FORWARD,
1948 .priority = 100,
1949 },
Julius Volz473b23d2008-09-02 15:55:54 +02001950#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951};
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001952/*
1953 * Initialize IP Virtual Server netns mem.
1954 */
1955static int __net_init __ip_vs_init(struct net *net)
1956{
1957 struct netns_ipvs *ipvs;
1958
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001959 ipvs = net_generic(net, ip_vs_net_id);
Joe Perches0a9ee812011-08-29 14:17:25 -07001960 if (ipvs == NULL)
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001961 return -ENOMEM;
Joe Perches0a9ee812011-08-29 14:17:25 -07001962
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001963 /* Hold the beast until a service is registerd */
1964 ipvs->enable = 0;
Hans Schillstromf6340ee2011-01-03 14:44:59 +01001965 ipvs->net = net;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001966 /* Counters used for creating unique names */
1967 ipvs->gen = atomic_read(&ipvs_netns_cnt);
1968 atomic_inc(&ipvs_netns_cnt);
1969 net->ipvs = ipvs;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001970
Hans Schillstrom503cf152011-05-01 18:50:16 +02001971 if (ip_vs_estimator_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001972 goto estimator_fail;
1973
Hans Schillstrom503cf152011-05-01 18:50:16 +02001974 if (ip_vs_control_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001975 goto control_fail;
1976
Hans Schillstrom503cf152011-05-01 18:50:16 +02001977 if (ip_vs_protocol_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001978 goto protocol_fail;
1979
Hans Schillstrom503cf152011-05-01 18:50:16 +02001980 if (ip_vs_app_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001981 goto app_fail;
1982
Hans Schillstrom503cf152011-05-01 18:50:16 +02001983 if (ip_vs_conn_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001984 goto conn_fail;
1985
Hans Schillstrom503cf152011-05-01 18:50:16 +02001986 if (ip_vs_sync_net_init(net) < 0)
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001987 goto sync_fail;
1988
Simon Hormana870c8c2011-02-01 18:21:53 +01001989 printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01001990 sizeof(struct netns_ipvs), ipvs->gen);
1991 return 0;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001992/*
1993 * Error handling
1994 */
1995
1996sync_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02001997 ip_vs_conn_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02001998conn_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02001999 ip_vs_app_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002000app_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002001 ip_vs_protocol_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002002protocol_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002003 ip_vs_control_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002004control_fail:
Hans Schillstrom503cf152011-05-01 18:50:16 +02002005 ip_vs_estimator_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002006estimator_fail:
Julian Anastasov39f618b2012-04-25 00:29:58 +03002007 net->ipvs = NULL;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002008 return -ENOMEM;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002009}
2010
2011static void __net_exit __ip_vs_cleanup(struct net *net)
2012{
Hans Schillstrom503cf152011-05-01 18:50:16 +02002013 ip_vs_service_net_cleanup(net); /* ip_vs_flush() with locks */
2014 ip_vs_conn_net_cleanup(net);
2015 ip_vs_app_net_cleanup(net);
2016 ip_vs_protocol_net_cleanup(net);
2017 ip_vs_control_net_cleanup(net);
2018 ip_vs_estimator_net_cleanup(net);
Hans Schillstrom1ae132b2011-05-03 22:09:30 +02002019 IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net)->gen);
Julian Anastasov39f618b2012-04-25 00:29:58 +03002020 net->ipvs = NULL;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002021}
2022
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002023static void __net_exit __ip_vs_dev_cleanup(struct net *net)
2024{
2025 EnterFunction(2);
2026 net_ipvs(net)->enable = 0; /* Disable packet reception */
Hans Schillstrom8f4e0a12011-06-13 09:06:57 +02002027 smp_wmb();
Hans Schillstrom503cf152011-05-01 18:50:16 +02002028 ip_vs_sync_net_cleanup(net);
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002029 LeaveFunction(2);
2030}
2031
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002032static struct pernet_operations ipvs_core_ops = {
2033 .init = __ip_vs_init,
2034 .exit = __ip_vs_cleanup,
2035 .id = &ip_vs_net_id,
2036 .size = sizeof(struct netns_ipvs),
2037};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002039static struct pernet_operations ipvs_core_dev_ops = {
2040 .exit = __ip_vs_dev_cleanup,
2041};
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043/*
2044 * Initialize IP Virtual Server
2045 */
2046static int __init ip_vs_init(void)
2047{
2048 int ret;
2049
2050 ret = ip_vs_control_init();
2051 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002052 pr_err("can't setup control.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002053 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 }
2055
2056 ip_vs_protocol_init();
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 ret = ip_vs_conn_init();
2059 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002060 pr_err("can't setup connection table.\n");
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002061 goto cleanup_protocol;
Hans Schillstrom61b1ab42011-01-03 14:44:42 +01002062 }
2063
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002064 ret = register_pernet_subsys(&ipvs_core_ops); /* Alloc ip_vs struct */
2065 if (ret < 0)
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002066 goto cleanup_conn;
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002067
2068 ret = register_pernet_device(&ipvs_core_dev_ops);
2069 if (ret < 0)
2070 goto cleanup_sub;
2071
Patrick McHardy41c5b312007-12-05 01:22:43 -08002072 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (ret < 0) {
Hannes Eder1e3e2382009-08-02 11:05:41 +00002074 pr_err("can't register hooks.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002075 goto cleanup_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077
Hans Schillstrom8537de82012-04-26 07:47:44 +02002078 ret = ip_vs_register_nl_ioctl();
2079 if (ret < 0) {
2080 pr_err("can't register netlink/ioctl.\n");
2081 goto cleanup_hooks;
2082 }
2083
Hannes Eder1e3e2382009-08-02 11:05:41 +00002084 pr_info("ipvs loaded.\n");
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return ret;
2087
Hans Schillstrom8537de82012-04-26 07:47:44 +02002088cleanup_hooks:
2089 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002090cleanup_dev:
2091 unregister_pernet_device(&ipvs_core_dev_ops);
2092cleanup_sub:
2093 unregister_pernet_subsys(&ipvs_core_ops);
Hans Schillstrom552ad652011-06-13 12:19:26 +02002094cleanup_conn:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 ip_vs_conn_cleanup();
Hans Schillstrom552ad652011-06-13 12:19:26 +02002096cleanup_protocol:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 ip_vs_protocol_cleanup();
2098 ip_vs_control_cleanup();
Hans Schillstrom6c8f7942011-06-13 12:19:27 +02002099exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return ret;
2101}
2102
2103static void __exit ip_vs_cleanup(void)
2104{
Hans Schillstrom8537de82012-04-26 07:47:44 +02002105 ip_vs_unregister_nl_ioctl();
Patrick McHardy41c5b312007-12-05 01:22:43 -08002106 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Hans Schillstrom7a4f0762011-05-03 22:09:31 +02002107 unregister_pernet_device(&ipvs_core_dev_ops);
2108 unregister_pernet_subsys(&ipvs_core_ops); /* free ip_vs struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 ip_vs_conn_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 ip_vs_protocol_cleanup();
2111 ip_vs_control_cleanup();
Hannes Eder1e3e2382009-08-02 11:05:41 +00002112 pr_info("ipvs unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
2115module_init(ip_vs_init);
2116module_exit(ip_vs_cleanup);
2117MODULE_LICENSE("GPL");