blob: 8dddb17a947a7be424d28e4f987939a02ff5dc54 [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
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/ip.h>
30#include <linux/tcp.h>
31#include <linux/icmp.h>
32
33#include <net/ip.h>
34#include <net/tcp.h>
35#include <net/udp.h>
36#include <net/icmp.h> /* for icmp_send */
37#include <net/route.h>
38
39#include <linux/netfilter.h>
40#include <linux/netfilter_ipv4.h>
41
Julius Volz2a3b7912008-09-02 15:55:47 +020042#ifdef CONFIG_IP_VS_IPV6
43#include <net/ipv6.h>
44#include <linux/netfilter_ipv6.h>
45#endif
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <net/ip_vs.h>
48
49
50EXPORT_SYMBOL(register_ip_vs_scheduler);
51EXPORT_SYMBOL(unregister_ip_vs_scheduler);
52EXPORT_SYMBOL(ip_vs_skb_replace);
53EXPORT_SYMBOL(ip_vs_proto_name);
54EXPORT_SYMBOL(ip_vs_conn_new);
55EXPORT_SYMBOL(ip_vs_conn_in_get);
56EXPORT_SYMBOL(ip_vs_conn_out_get);
57#ifdef CONFIG_IP_VS_PROTO_TCP
58EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
59#endif
60EXPORT_SYMBOL(ip_vs_conn_put);
61#ifdef CONFIG_IP_VS_DEBUG
62EXPORT_SYMBOL(ip_vs_get_debug_level);
63#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65
66/* ID used in ICMP lookups */
67#define icmp_id(icmph) (((icmph)->un).echo.id)
Julius Volz2a3b7912008-09-02 15:55:47 +020068#define icmpv6_id(icmph) (icmph->icmp6_dataun.u_echo.identifier)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70const char *ip_vs_proto_name(unsigned proto)
71{
72 static char buf[20];
73
74 switch (proto) {
75 case IPPROTO_IP:
76 return "IP";
77 case IPPROTO_UDP:
78 return "UDP";
79 case IPPROTO_TCP:
80 return "TCP";
81 case IPPROTO_ICMP:
82 return "ICMP";
Julius Volz2a3b7912008-09-02 15:55:47 +020083#ifdef CONFIG_IP_VS_IPV6
84 case IPPROTO_ICMPV6:
85 return "ICMPv6";
86#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 default:
88 sprintf(buf, "IP_%d", proto);
89 return buf;
90 }
91}
92
93void ip_vs_init_hash_table(struct list_head *table, int rows)
94{
95 while (--rows >= 0)
96 INIT_LIST_HEAD(&table[rows]);
97}
98
99static inline void
100ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
101{
102 struct ip_vs_dest *dest = cp->dest;
103 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
104 spin_lock(&dest->stats.lock);
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200105 dest->stats.ustats.inpkts++;
106 dest->stats.ustats.inbytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 spin_unlock(&dest->stats.lock);
108
109 spin_lock(&dest->svc->stats.lock);
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200110 dest->svc->stats.ustats.inpkts++;
111 dest->svc->stats.ustats.inbytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 spin_unlock(&dest->svc->stats.lock);
113
114 spin_lock(&ip_vs_stats.lock);
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200115 ip_vs_stats.ustats.inpkts++;
116 ip_vs_stats.ustats.inbytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 spin_unlock(&ip_vs_stats.lock);
118 }
119}
120
121
122static inline void
123ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
124{
125 struct ip_vs_dest *dest = cp->dest;
126 if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
127 spin_lock(&dest->stats.lock);
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200128 dest->stats.ustats.outpkts++;
129 dest->stats.ustats.outbytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 spin_unlock(&dest->stats.lock);
131
132 spin_lock(&dest->svc->stats.lock);
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200133 dest->svc->stats.ustats.outpkts++;
134 dest->svc->stats.ustats.outbytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 spin_unlock(&dest->svc->stats.lock);
136
137 spin_lock(&ip_vs_stats.lock);
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200138 ip_vs_stats.ustats.outpkts++;
139 ip_vs_stats.ustats.outbytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 spin_unlock(&ip_vs_stats.lock);
141 }
142}
143
144
145static inline void
146ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
147{
148 spin_lock(&cp->dest->stats.lock);
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200149 cp->dest->stats.ustats.conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 spin_unlock(&cp->dest->stats.lock);
151
152 spin_lock(&svc->stats.lock);
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200153 svc->stats.ustats.conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 spin_unlock(&svc->stats.lock);
155
156 spin_lock(&ip_vs_stats.lock);
Sven Wegenere9c0ce22008-09-08 13:39:04 +0200157 ip_vs_stats.ustats.conns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 spin_unlock(&ip_vs_stats.lock);
159}
160
161
162static inline int
163ip_vs_set_state(struct ip_vs_conn *cp, int direction,
164 const struct sk_buff *skb,
165 struct ip_vs_protocol *pp)
166{
167 if (unlikely(!pp->state_transition))
168 return 0;
169 return pp->state_transition(cp, direction, skb, pp);
170}
171
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
174 * IPVS persistent scheduling function
175 * It creates a connection entry according to its template if exists,
176 * or selects a server and creates a connection entry plus a template.
177 * Locking: we are svc user (svc->refcnt), so we hold all dests too
178 * Protocols supported: TCP, UDP
179 */
180static struct ip_vs_conn *
181ip_vs_sched_persist(struct ip_vs_service *svc,
182 const struct sk_buff *skb,
Al Viro014d7302006-09-28 14:29:52 -0700183 __be16 ports[2])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct ip_vs_conn *cp = NULL;
Julius Volz28364a52008-09-02 15:55:43 +0200186 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 struct ip_vs_dest *dest;
188 struct ip_vs_conn *ct;
Julius Volzcd17f9e2008-09-02 15:55:46 +0200189 __be16 dport; /* destination port to forward */
Julius Volz28364a52008-09-02 15:55:43 +0200190 union nf_inet_addr snet; /* source network of the client,
191 after masking */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200192
193 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /* Mask saddr with the netmask to adjust template granularity */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200196#ifdef CONFIG_IP_VS_IPV6
197 if (svc->af == AF_INET6)
198 ipv6_addr_prefix(&snet.in6, &iph.saddr.in6, svc->netmask);
199 else
200#endif
201 snet.ip = iph.saddr.ip & svc->netmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Julius Volzcd17f9e2008-09-02 15:55:46 +0200203 IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
204 "mnet %s\n",
205 IP_VS_DBG_ADDR(svc->af, &iph.saddr), ntohs(ports[0]),
206 IP_VS_DBG_ADDR(svc->af, &iph.daddr), ntohs(ports[1]),
207 IP_VS_DBG_ADDR(svc->af, &snet));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /*
210 * As far as we know, FTP is a very complicated network protocol, and
211 * it uses control connection and data connections. For active FTP,
212 * FTP server initialize data connection to the client, its source port
213 * is often 20. For passive FTP, FTP server tells the clients the port
214 * that it passively listens to, and the client issues the data
215 * connection. In the tunneling or direct routing mode, the load
216 * balancer is on the client-to-server half of connection, the port
217 * number is unknown to the load balancer. So, a conn template like
218 * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
219 * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
220 * is created for other persistent services.
221 */
222 if (ports[1] == svc->port) {
223 /* Check if a template already exists */
224 if (svc->port != FTPPORT)
Julius Volzcd17f9e2008-09-02 15:55:46 +0200225 ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
Julius Volz28364a52008-09-02 15:55:43 +0200226 &iph.daddr, ports[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 else
Julius Volzcd17f9e2008-09-02 15:55:46 +0200228 ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
Julius Volz28364a52008-09-02 15:55:43 +0200229 &iph.daddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 if (!ct || !ip_vs_check_template(ct)) {
232 /*
233 * No template found or the dest of the connection
234 * template is not available.
235 */
236 dest = svc->scheduler->schedule(svc, skb);
237 if (dest == NULL) {
238 IP_VS_DBG(1, "p-schedule: no dest found.\n");
239 return NULL;
240 }
241
242 /*
243 * Create a template like <protocol,caddr,0,
244 * vaddr,vport,daddr,dport> for non-ftp service,
245 * and <protocol,caddr,0,vaddr,0,daddr,0>
246 * for ftp service.
247 */
248 if (svc->port != FTPPORT)
Julius Volzcd17f9e2008-09-02 15:55:46 +0200249 ct = ip_vs_conn_new(svc->af, iph.protocol,
Julius Volz28364a52008-09-02 15:55:43 +0200250 &snet, 0,
251 &iph.daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 ports[1],
Julius Volz28364a52008-09-02 15:55:43 +0200253 &dest->addr, dest->port,
Julian Anastasov87375ab2005-09-14 21:08:51 -0700254 IP_VS_CONN_F_TEMPLATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 dest);
256 else
Julius Volzcd17f9e2008-09-02 15:55:46 +0200257 ct = ip_vs_conn_new(svc->af, iph.protocol,
Julius Volz28364a52008-09-02 15:55:43 +0200258 &snet, 0,
259 &iph.daddr, 0,
260 &dest->addr, 0,
Julian Anastasov87375ab2005-09-14 21:08:51 -0700261 IP_VS_CONN_F_TEMPLATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 dest);
263 if (ct == NULL)
264 return NULL;
265
266 ct->timeout = svc->timeout;
267 } else {
268 /* set destination with the found template */
269 dest = ct->dest;
270 }
271 dport = dest->port;
272 } else {
273 /*
274 * Note: persistent fwmark-based services and persistent
275 * port zero service are handled here.
276 * fwmark template: <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
277 * port zero template: <protocol,caddr,0,vaddr,0,daddr,0>
278 */
Julius Volz28364a52008-09-02 15:55:43 +0200279 if (svc->fwmark) {
280 union nf_inet_addr fwmark = {
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000281 .ip = htonl(svc->fwmark)
Julius Volz28364a52008-09-02 15:55:43 +0200282 };
283
Julius Volzcd17f9e2008-09-02 15:55:46 +0200284 ct = ip_vs_ct_in_get(svc->af, IPPROTO_IP, &snet, 0,
Julius Volz28364a52008-09-02 15:55:43 +0200285 &fwmark, 0);
286 } else
Julius Volzcd17f9e2008-09-02 15:55:46 +0200287 ct = ip_vs_ct_in_get(svc->af, iph.protocol, &snet, 0,
Julius Volz28364a52008-09-02 15:55:43 +0200288 &iph.daddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if (!ct || !ip_vs_check_template(ct)) {
291 /*
292 * If it is not persistent port zero, return NULL,
293 * otherwise create a connection template.
294 */
295 if (svc->port)
296 return NULL;
297
298 dest = svc->scheduler->schedule(svc, skb);
299 if (dest == NULL) {
300 IP_VS_DBG(1, "p-schedule: no dest found.\n");
301 return NULL;
302 }
303
304 /*
305 * Create a template according to the service
306 */
Julius Volz28364a52008-09-02 15:55:43 +0200307 if (svc->fwmark) {
308 union nf_inet_addr fwmark = {
Simon Hormanbe8be9e2009-05-06 15:02:29 +0000309 .ip = htonl(svc->fwmark)
Julius Volz28364a52008-09-02 15:55:43 +0200310 };
311
Julius Volzcd17f9e2008-09-02 15:55:46 +0200312 ct = ip_vs_conn_new(svc->af, IPPROTO_IP,
Julius Volz28364a52008-09-02 15:55:43 +0200313 &snet, 0,
314 &fwmark, 0,
315 &dest->addr, 0,
Julian Anastasov87375ab2005-09-14 21:08:51 -0700316 IP_VS_CONN_F_TEMPLATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 dest);
Julius Volz28364a52008-09-02 15:55:43 +0200318 } else
Julius Volzcd17f9e2008-09-02 15:55:46 +0200319 ct = ip_vs_conn_new(svc->af, iph.protocol,
Julius Volz28364a52008-09-02 15:55:43 +0200320 &snet, 0,
321 &iph.daddr, 0,
322 &dest->addr, 0,
Julian Anastasov87375ab2005-09-14 21:08:51 -0700323 IP_VS_CONN_F_TEMPLATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 dest);
325 if (ct == NULL)
326 return NULL;
327
328 ct->timeout = svc->timeout;
329 } else {
330 /* set destination with the found template */
331 dest = ct->dest;
332 }
333 dport = ports[1];
334 }
335
336 /*
337 * Create a new connection according to the template
338 */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200339 cp = ip_vs_conn_new(svc->af, iph.protocol,
Julius Volz28364a52008-09-02 15:55:43 +0200340 &iph.saddr, ports[0],
341 &iph.daddr, ports[1],
342 &dest->addr, dport,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 0,
344 dest);
345 if (cp == NULL) {
346 ip_vs_conn_put(ct);
347 return NULL;
348 }
349
350 /*
351 * Add its control
352 */
353 ip_vs_control_add(cp, ct);
354 ip_vs_conn_put(ct);
355
356 ip_vs_conn_stats(cp, svc);
357 return cp;
358}
359
360
361/*
362 * IPVS main scheduling function
363 * It selects a server according to the virtual service, and
364 * creates a connection entry.
365 * Protocols supported: TCP, UDP
366 */
367struct ip_vs_conn *
368ip_vs_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
369{
370 struct ip_vs_conn *cp = NULL;
Julius Volz28364a52008-09-02 15:55:43 +0200371 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct ip_vs_dest *dest;
Al Viro014d7302006-09-28 14:29:52 -0700373 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Julius Volz28364a52008-09-02 15:55:43 +0200375 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
376 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (pptr == NULL)
378 return NULL;
379
380 /*
381 * Persistent service
382 */
383 if (svc->flags & IP_VS_SVC_F_PERSISTENT)
384 return ip_vs_sched_persist(svc, skb, pptr);
385
386 /*
387 * Non-persistent service
388 */
389 if (!svc->fwmark && pptr[1] != svc->port) {
390 if (!svc->port)
391 IP_VS_ERR("Schedule: port zero only supported "
392 "in persistent services, "
393 "check your ipvs configuration\n");
394 return NULL;
395 }
396
397 dest = svc->scheduler->schedule(svc, skb);
398 if (dest == NULL) {
399 IP_VS_DBG(1, "Schedule: no dest found.\n");
400 return NULL;
401 }
402
403 /*
404 * Create a connection entry.
405 */
Julius Volzcd17f9e2008-09-02 15:55:46 +0200406 cp = ip_vs_conn_new(svc->af, iph.protocol,
Julius Volz28364a52008-09-02 15:55:43 +0200407 &iph.saddr, pptr[0],
408 &iph.daddr, pptr[1],
409 &dest->addr, dest->port ? dest->port : pptr[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 0,
411 dest);
412 if (cp == NULL)
413 return NULL;
414
Julius Volzcd17f9e2008-09-02 15:55:46 +0200415 IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
416 "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
417 ip_vs_fwd_tag(cp),
418 IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
419 IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
420 IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
421 cp->flags, atomic_read(&cp->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 ip_vs_conn_stats(cp, svc);
424 return cp;
425}
426
427
428/*
429 * Pass or drop the packet.
430 * Called by ip_vs_in, when the virtual service is available but
431 * no destination is available for a new connection.
432 */
433int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
434 struct ip_vs_protocol *pp)
435{
Al Viro014d7302006-09-28 14:29:52 -0700436 __be16 _ports[2], *pptr;
Julius Volz28364a52008-09-02 15:55:43 +0200437 struct ip_vs_iphdr iph;
Julius Volz2a3b7912008-09-02 15:55:47 +0200438 int unicast;
439 ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Julius Volz28364a52008-09-02 15:55:43 +0200441 pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (pptr == NULL) {
443 ip_vs_service_put(svc);
444 return NF_DROP;
445 }
446
Julius Volz2a3b7912008-09-02 15:55:47 +0200447#ifdef CONFIG_IP_VS_IPV6
448 if (svc->af == AF_INET6)
449 unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST;
450 else
451#endif
452 unicast = (inet_addr_type(&init_net, iph.daddr.ip) == RTN_UNICAST);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* if it is fwmark-based service, the cache_bypass sysctl is up
Julius Volz2a3b7912008-09-02 15:55:47 +0200455 and the destination is a non-local unicast, then create
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 a cache_bypass connection entry */
Julius Volz2a3b7912008-09-02 15:55:47 +0200457 if (sysctl_ip_vs_cache_bypass && svc->fwmark && unicast) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int ret, cs;
459 struct ip_vs_conn *cp;
Simon Hormandff630d2008-09-17 10:10:42 +1000460 union nf_inet_addr daddr = { .all = { 0, 0, 0, 0 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 ip_vs_service_put(svc);
463
464 /* create a new connection entry */
465 IP_VS_DBG(6, "ip_vs_leave: create a cache_bypass entry\n");
Julius Volz2a3b7912008-09-02 15:55:47 +0200466 cp = ip_vs_conn_new(svc->af, iph.protocol,
Julius Volz28364a52008-09-02 15:55:43 +0200467 &iph.saddr, pptr[0],
468 &iph.daddr, pptr[1],
Simon Hormandff630d2008-09-17 10:10:42 +1000469 &daddr, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 IP_VS_CONN_F_BYPASS,
471 NULL);
472 if (cp == NULL)
473 return NF_DROP;
474
475 /* statistics */
476 ip_vs_in_stats(cp, skb);
477
478 /* set state */
479 cs = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
480
481 /* transmit the first SYN packet */
482 ret = cp->packet_xmit(skb, cp, pp);
483 /* do not touch skb anymore */
484
485 atomic_inc(&cp->in_pkts);
486 ip_vs_conn_put(cp);
487 return ret;
488 }
489
490 /*
491 * When the virtual ftp service is presented, packets destined
492 * for other services on the VIP may get here (except services
493 * listed in the ipvs table), pass the packets, because it is
494 * not ipvs job to decide to drop the packets.
495 */
496 if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
497 ip_vs_service_put(svc);
498 return NF_ACCEPT;
499 }
500
501 ip_vs_service_put(svc);
502
503 /*
504 * Notify the client that the destination is unreachable, and
505 * release the socket buffer.
506 * Since it is in IP layer, the TCP socket is not actually
507 * created, the TCP RST packet cannot be sent, instead that
508 * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
509 */
Julius Volz2a3b7912008-09-02 15:55:47 +0200510#ifdef CONFIG_IP_VS_IPV6
511 if (svc->af == AF_INET6)
512 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0,
513 skb->dev);
514 else
515#endif
516 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return NF_DROP;
519}
520
521
522/*
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800523 * It is hooked before NF_IP_PRI_NAT_SRC at the NF_INET_POST_ROUTING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * chain, and is used for VS/NAT.
525 * It detects packets for VS/NAT connections and sends the packets
526 * immediately. This can avoid that iptable_nat mangles the packets
527 * for VS/NAT.
528 */
529static unsigned int ip_vs_post_routing(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700530 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 const struct net_device *in,
532 const struct net_device *out,
533 int (*okfn)(struct sk_buff *))
534{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700535 if (!skb->ipvs_property)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* The packet was sent from IPVS, exit this chain */
Patrick McHardyabbcc732006-01-05 12:20:40 -0800538 return NF_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Al Virob1550f22006-11-14 21:37:50 -0800541__sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Al Virod3bc23e2006-11-14 21:24:49 -0800543 return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Herbert Xu776c7292007-10-14 00:38:32 -0700546static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Herbert Xu776c7292007-10-14 00:38:32 -0700548 int err = ip_defrag(skb, user);
549
550 if (!err)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700551 ip_send_check(ip_hdr(skb));
Herbert Xu776c7292007-10-14 00:38:32 -0700552
553 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Julius Volz2a3b7912008-09-02 15:55:47 +0200556#ifdef CONFIG_IP_VS_IPV6
557static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user)
558{
559 /* TODO IPv6: Find out what to do here for IPv6 */
560 return 0;
561}
562#endif
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
565 * Packet has been made sufficiently writable in caller
566 * - inout: 1=in->out, 0=out->in
567 */
568void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
569 struct ip_vs_conn *cp, int inout)
570{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700571 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 unsigned int icmp_offset = iph->ihl*4;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700573 struct icmphdr *icmph = (struct icmphdr *)(skb_network_header(skb) +
574 icmp_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct iphdr *ciph = (struct iphdr *)(icmph + 1);
576
577 if (inout) {
Julius Volze7ade462008-09-02 15:55:33 +0200578 iph->saddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200580 ciph->daddr = cp->vaddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 ip_send_check(ciph);
582 } else {
Julius Volze7ade462008-09-02 15:55:33 +0200583 iph->daddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 ip_send_check(iph);
Julius Volze7ade462008-09-02 15:55:33 +0200585 ciph->saddr = cp->daddr.ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 ip_send_check(ciph);
587 }
588
589 /* the TCP/UDP port */
590 if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol) {
Al Viro014d7302006-09-28 14:29:52 -0700591 __be16 *ports = (void *)ciph + ciph->ihl*4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 if (inout)
594 ports[1] = cp->vport;
595 else
596 ports[0] = cp->dport;
597 }
598
599 /* And finally the ICMP checksum */
600 icmph->checksum = 0;
601 icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
602 skb->ip_summed = CHECKSUM_UNNECESSARY;
603
604 if (inout)
605 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
606 "Forwarding altered outgoing ICMP");
607 else
608 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
609 "Forwarding altered incoming ICMP");
610}
611
Julius Volzb3cdd2a72008-09-02 15:55:45 +0200612#ifdef CONFIG_IP_VS_IPV6
613void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
614 struct ip_vs_conn *cp, int inout)
615{
616 struct ipv6hdr *iph = ipv6_hdr(skb);
617 unsigned int icmp_offset = sizeof(struct ipv6hdr);
618 struct icmp6hdr *icmph = (struct icmp6hdr *)(skb_network_header(skb) +
619 icmp_offset);
620 struct ipv6hdr *ciph = (struct ipv6hdr *)(icmph + 1);
621
622 if (inout) {
623 iph->saddr = cp->vaddr.in6;
624 ciph->daddr = cp->vaddr.in6;
625 } else {
626 iph->daddr = cp->daddr.in6;
627 ciph->saddr = cp->daddr.in6;
628 }
629
630 /* the TCP/UDP port */
631 if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr) {
632 __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr);
633
634 if (inout)
635 ports[1] = cp->vport;
636 else
637 ports[0] = cp->dport;
638 }
639
640 /* And finally the ICMP checksum */
641 icmph->icmp6_cksum = 0;
642 /* TODO IPv6: is this correct for ICMPv6? */
643 ip_vs_checksum_complete(skb, icmp_offset);
644 skb->ip_summed = CHECKSUM_UNNECESSARY;
645
646 if (inout)
647 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
648 "Forwarding altered outgoing ICMPv6");
649 else
650 IP_VS_DBG_PKT(11, pp, skb, (void *)ciph - (void *)iph,
651 "Forwarding altered incoming ICMPv6");
652}
653#endif
654
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000655/* Handle relevant response ICMP messages - forward to the right
656 * destination host. Used for NAT and local client.
657 */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000658static int handle_response_icmp(int af, struct sk_buff *skb,
659 union nf_inet_addr *snet,
660 __u8 protocol, struct ip_vs_conn *cp,
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000661 struct ip_vs_protocol *pp,
662 unsigned int offset, unsigned int ihl)
663{
664 unsigned int verdict = NF_DROP;
665
666 if (IP_VS_FWD_METHOD(cp) != 0) {
667 IP_VS_ERR("shouldn't reach here, because the box is on the "
668 "half connection in the tun/dr module.\n");
669 }
670
671 /* Ensure the checksum is correct */
672 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
673 /* Failed checksum! */
Simon Hormanf2428ed2008-09-05 11:17:14 +1000674 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
675 IP_VS_DBG_ADDR(af, snet));
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000676 goto out;
677 }
678
Simon Hormanf2428ed2008-09-05 11:17:14 +1000679 if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol)
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000680 offset += 2 * sizeof(__u16);
681 if (!skb_make_writable(skb, offset))
682 goto out;
683
Simon Hormanf2428ed2008-09-05 11:17:14 +1000684#ifdef CONFIG_IP_VS_IPV6
685 if (af == AF_INET6)
686 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
687 else
688#endif
689 ip_vs_nat_icmp(skb, pp, cp, 1);
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000690
691 /* do the statistics and put it back */
692 ip_vs_out_stats(cp, skb);
693
694 skb->ipvs_property = 1;
695 verdict = NF_ACCEPT;
696
697out:
698 __ip_vs_conn_put(cp);
699
700 return verdict;
701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/*
704 * Handle ICMP messages in the inside-to-outside direction (outgoing).
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000705 * Find any that might be relevant, check against existing connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * Currently handles error types - unreachable, quench, ttl exceeded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700708static int ip_vs_out_icmp(struct sk_buff *skb, int *related)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 struct iphdr *iph;
711 struct icmphdr _icmph, *ic;
712 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +0200713 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct ip_vs_conn *cp;
715 struct ip_vs_protocol *pp;
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000716 unsigned int offset, ihl;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000717 union nf_inet_addr snet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 *related = 1;
720
721 /* reassemble IP fragments */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700722 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
Herbert Xu776c7292007-10-14 00:38:32 -0700723 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700727 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 offset = ihl = iph->ihl * 4;
729 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
730 if (ic == NULL)
731 return NF_DROP;
732
Harvey Harrison14d5e832008-10-31 00:54:29 -0700733 IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -0700735 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /*
738 * Work through seeing if this is for us.
739 * These checks are supposed to be in an order that means easy
740 * things are checked first to speed up processing.... however
741 * this means that some packets will manage to get a long way
742 * down this stack and then be rejected, but that's life.
743 */
744 if ((ic->type != ICMP_DEST_UNREACH) &&
745 (ic->type != ICMP_SOURCE_QUENCH) &&
746 (ic->type != ICMP_TIME_EXCEEDED)) {
747 *related = 0;
748 return NF_ACCEPT;
749 }
750
751 /* Now find the contained IP header */
752 offset += sizeof(_icmph);
753 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
754 if (cih == NULL)
755 return NF_ACCEPT; /* The packet looks wrong, ignore */
756
757 pp = ip_vs_proto_get(cih->protocol);
758 if (!pp)
759 return NF_ACCEPT;
760
761 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +0900762 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 pp->dont_defrag))
764 return NF_ACCEPT;
765
766 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMP for");
767
768 offset += cih->ihl * 4;
769
Julius Volz51ef3482008-09-02 15:55:40 +0200770 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* The embedded headers contain source and dest in reverse order */
Julius Volz51ef3482008-09-02 15:55:40 +0200772 cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (!cp)
774 return NF_ACCEPT;
775
Simon Hormanf2428ed2008-09-05 11:17:14 +1000776 snet.ip = iph->saddr;
777 return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
778 pp, offset, ihl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Julius Volz2a3b7912008-09-02 15:55:47 +0200781#ifdef CONFIG_IP_VS_IPV6
782static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related)
783{
784 struct ipv6hdr *iph;
785 struct icmp6hdr _icmph, *ic;
786 struct ipv6hdr _ciph, *cih; /* The ip header contained
787 within the ICMP */
788 struct ip_vs_iphdr ciph;
789 struct ip_vs_conn *cp;
790 struct ip_vs_protocol *pp;
Simon Hormanf2428ed2008-09-05 11:17:14 +1000791 unsigned int offset;
792 union nf_inet_addr snet;
Julius Volz2a3b7912008-09-02 15:55:47 +0200793
794 *related = 1;
795
796 /* reassemble IP fragments */
797 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
798 if (ip_vs_gather_frags_v6(skb, IP_DEFRAG_VS_OUT))
799 return NF_STOLEN;
800 }
801
802 iph = ipv6_hdr(skb);
803 offset = sizeof(struct ipv6hdr);
804 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
805 if (ic == NULL)
806 return NF_DROP;
807
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700808 IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) %pI6->%pI6\n",
Julius Volz2a3b7912008-09-02 15:55:47 +0200809 ic->icmp6_type, ntohs(icmpv6_id(ic)),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -0700810 &iph->saddr, &iph->daddr);
Julius Volz2a3b7912008-09-02 15:55:47 +0200811
812 /*
813 * Work through seeing if this is for us.
814 * These checks are supposed to be in an order that means easy
815 * things are checked first to speed up processing.... however
816 * this means that some packets will manage to get a long way
817 * down this stack and then be rejected, but that's life.
818 */
819 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
820 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
821 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
822 *related = 0;
823 return NF_ACCEPT;
824 }
825
826 /* Now find the contained IP header */
827 offset += sizeof(_icmph);
828 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
829 if (cih == NULL)
830 return NF_ACCEPT; /* The packet looks wrong, ignore */
831
832 pp = ip_vs_proto_get(cih->nexthdr);
833 if (!pp)
834 return NF_ACCEPT;
835
836 /* Is the embedded protocol header present? */
837 /* TODO: we don't support fragmentation at the moment anyways */
838 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
839 return NF_ACCEPT;
840
841 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking outgoing ICMPv6 for");
842
843 offset += sizeof(struct ipv6hdr);
844
845 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
846 /* The embedded headers contain source and dest in reverse order */
847 cp = pp->conn_out_get(AF_INET6, skb, pp, &ciph, offset, 1);
848 if (!cp)
849 return NF_ACCEPT;
850
Simon Horman178f5e42008-09-08 09:34:46 +1000851 ipv6_addr_copy(&snet.in6, &iph->saddr);
Simon Hormanf2428ed2008-09-05 11:17:14 +1000852 return handle_response_icmp(AF_INET6, skb, &snet, cih->nexthdr, cp,
853 pp, offset, sizeof(struct ipv6hdr));
Julius Volz2a3b7912008-09-02 15:55:47 +0200854}
855#endif
856
857static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 struct tcphdr _tcph, *th;
860
Julius Volz2a3b7912008-09-02 15:55:47 +0200861 th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (th == NULL)
863 return 0;
864 return th->rst;
865}
866
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000867/* Handle response packets: rewrite addresses and send away...
868 * Used for NAT and local client.
869 */
870static unsigned int
871handle_response(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
872 struct ip_vs_conn *cp, int ihl)
873{
874 IP_VS_DBG_PKT(11, pp, skb, 0, "Outgoing packet");
875
876 if (!skb_make_writable(skb, ihl))
877 goto drop;
878
879 /* mangle the packet */
880 if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
881 goto drop;
882
883#ifdef CONFIG_IP_VS_IPV6
884 if (af == AF_INET6)
885 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
886 else
887#endif
888 {
889 ip_hdr(skb)->saddr = cp->vaddr.ip;
890 ip_send_check(ip_hdr(skb));
891 }
892
893 /* For policy routing, packets originating from this
894 * machine itself may be routed differently to packets
895 * passing through. We want this packet to be routed as
896 * if it came from this machine itself. So re-compute
897 * the routing information.
898 */
899#ifdef CONFIG_IP_VS_IPV6
900 if (af == AF_INET6) {
901 if (ip6_route_me_harder(skb) != 0)
902 goto drop;
903 } else
904#endif
905 if (ip_route_me_harder(skb, RTN_LOCAL) != 0)
906 goto drop;
907
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000908 IP_VS_DBG_PKT(10, pp, skb, 0, "After SNAT");
909
910 ip_vs_out_stats(cp, skb);
911 ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pp);
912 ip_vs_conn_put(cp);
913
914 skb->ipvs_property = 1;
915
916 LeaveFunction(11);
917 return NF_ACCEPT;
918
919drop:
920 ip_vs_conn_put(cp);
921 kfree_skb(skb);
922 return NF_STOLEN;
923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800926 * It is hooked at the NF_INET_FORWARD chain, used only for VS/NAT.
Malcolm Turnbull4856c842008-09-05 11:17:13 +1000927 * Check if outgoing packet belongs to the established ip_vs_conn.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 */
929static unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700930ip_vs_out(unsigned int hooknum, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 const struct net_device *in, const struct net_device *out,
932 int (*okfn)(struct sk_buff *))
933{
Julius Volz51ef3482008-09-02 15:55:40 +0200934 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 struct ip_vs_protocol *pp;
936 struct ip_vs_conn *cp;
Julius Volz2a3b7912008-09-02 15:55:47 +0200937 int af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 EnterFunction(11);
940
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700941 af = (skb->protocol == htons(ETH_P_IP)) ? AF_INET : AF_INET6;
Julius Volz2a3b7912008-09-02 15:55:47 +0200942
Harald Welte6869c4d2005-08-09 19:24:19 -0700943 if (skb->ipvs_property)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return NF_ACCEPT;
945
Julius Volz2a3b7912008-09-02 15:55:47 +0200946 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
947#ifdef CONFIG_IP_VS_IPV6
948 if (af == AF_INET6) {
949 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
950 int related, verdict = ip_vs_out_icmp_v6(skb, &related);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Julius Volz2a3b7912008-09-02 15:55:47 +0200952 if (related)
953 return verdict;
954 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
955 }
956 } else
957#endif
958 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
959 int related, verdict = ip_vs_out_icmp(skb, &related);
960
961 if (related)
962 return verdict;
963 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Julius Volz51ef3482008-09-02 15:55:40 +0200966 pp = ip_vs_proto_get(iph.protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (unlikely(!pp))
968 return NF_ACCEPT;
969
970 /* reassemble IP fragments */
Julius Volz2a3b7912008-09-02 15:55:47 +0200971#ifdef CONFIG_IP_VS_IPV6
972 if (af == AF_INET6) {
973 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
974 int related, verdict = ip_vs_out_icmp_v6(skb, &related);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Julius Volz2a3b7912008-09-02 15:55:47 +0200976 if (related)
977 return verdict;
978
979 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
980 }
981 } else
982#endif
983 if (unlikely(ip_hdr(skb)->frag_off & htons(IP_MF|IP_OFFSET) &&
984 !pp->dont_defrag)) {
985 if (ip_vs_gather_frags(skb, IP_DEFRAG_VS_OUT))
986 return NF_STOLEN;
987
988 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /*
992 * Check if the packet belongs to an existing entry
993 */
Julius Volz2a3b7912008-09-02 15:55:47 +0200994 cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (unlikely(!cp)) {
997 if (sysctl_ip_vs_nat_icmp_send &&
998 (pp->protocol == IPPROTO_TCP ||
999 pp->protocol == IPPROTO_UDP)) {
Al Viro014d7302006-09-28 14:29:52 -07001000 __be16 _ports[2], *pptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Julius Volz51ef3482008-09-02 15:55:40 +02001002 pptr = skb_header_pointer(skb, iph.len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 sizeof(_ports), _ports);
1004 if (pptr == NULL)
1005 return NF_ACCEPT; /* Not for me */
Julius Volz7937df12008-09-02 15:55:48 +02001006 if (ip_vs_lookup_real_service(af, iph.protocol,
1007 &iph.saddr,
Julius Volz2a3b7912008-09-02 15:55:47 +02001008 pptr[0])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /*
1010 * Notify the real server: there is no
1011 * existing entry if it is not RST
1012 * packet or not TCP packet.
1013 */
Julius Volz51ef3482008-09-02 15:55:40 +02001014 if (iph.protocol != IPPROTO_TCP
Julius Volz2a3b7912008-09-02 15:55:47 +02001015 || !is_tcp_reset(skb, iph.len)) {
1016#ifdef CONFIG_IP_VS_IPV6
1017 if (af == AF_INET6)
1018 icmpv6_send(skb,
1019 ICMPV6_DEST_UNREACH,
1020 ICMPV6_PORT_UNREACH,
1021 0, skb->dev);
1022 else
1023#endif
1024 icmp_send(skb,
1025 ICMP_DEST_UNREACH,
1026 ICMP_PORT_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return NF_DROP;
Simon Horman5af149c2008-09-08 09:34:45 +10001028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030 }
1031 IP_VS_DBG_PKT(12, pp, skb, 0,
1032 "packet continues traversal as normal");
1033 return NF_ACCEPT;
1034 }
1035
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001036 return handle_response(af, skb, pp, cp, iph.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039
1040/*
1041 * Handle ICMP messages in the outside-to-inside direction (incoming).
1042 * Find any that might be relevant, check against existing connections,
1043 * forward to the right destination host if relevant.
1044 * Currently handles error types - unreachable, quench, ttl exceeded.
1045 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001046static int
Herbert Xu3db05fe2007-10-15 00:53:15 -07001047ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 struct iphdr *iph;
1050 struct icmphdr _icmph, *ic;
1051 struct iphdr _ciph, *cih; /* The ip header contained within the ICMP */
Julius Volz51ef3482008-09-02 15:55:40 +02001052 struct ip_vs_iphdr ciph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct ip_vs_conn *cp;
1054 struct ip_vs_protocol *pp;
1055 unsigned int offset, ihl, verdict;
Simon Hormanf2428ed2008-09-05 11:17:14 +10001056 union nf_inet_addr snet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 *related = 1;
1059
1060 /* reassemble IP fragments */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001061 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001062 if (ip_vs_gather_frags(skb, hooknum == NF_INET_LOCAL_IN ?
Herbert Xu776c7292007-10-14 00:38:32 -07001063 IP_DEFRAG_VS_IN : IP_DEFRAG_VS_FWD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return NF_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001067 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 offset = ihl = iph->ihl * 4;
1069 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1070 if (ic == NULL)
1071 return NF_DROP;
1072
Harvey Harrison14d5e832008-10-31 00:54:29 -07001073 IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 ic->type, ntohs(icmp_id(ic)),
Harvey Harrison14d5e832008-10-31 00:54:29 -07001075 &iph->saddr, &iph->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /*
1078 * Work through seeing if this is for us.
1079 * These checks are supposed to be in an order that means easy
1080 * things are checked first to speed up processing.... however
1081 * this means that some packets will manage to get a long way
1082 * down this stack and then be rejected, but that's life.
1083 */
1084 if ((ic->type != ICMP_DEST_UNREACH) &&
1085 (ic->type != ICMP_SOURCE_QUENCH) &&
1086 (ic->type != ICMP_TIME_EXCEEDED)) {
1087 *related = 0;
1088 return NF_ACCEPT;
1089 }
1090
1091 /* Now find the contained IP header */
1092 offset += sizeof(_icmph);
1093 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1094 if (cih == NULL)
1095 return NF_ACCEPT; /* The packet looks wrong, ignore */
1096
1097 pp = ip_vs_proto_get(cih->protocol);
1098 if (!pp)
1099 return NF_ACCEPT;
1100
1101 /* Is the embedded protocol header present? */
YOSHIFUJI Hideaki4412ec42007-03-07 14:19:10 +09001102 if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 pp->dont_defrag))
1104 return NF_ACCEPT;
1105
1106 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMP for");
1107
1108 offset += cih->ihl * 4;
1109
Julius Volz51ef3482008-09-02 15:55:40 +02001110 ip_vs_fill_iphdr(AF_INET, cih, &ciph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* The embedded headers contain source and dest in reverse order */
Julius Volz51ef3482008-09-02 15:55:40 +02001112 cp = pp->conn_in_get(AF_INET, skb, pp, &ciph, offset, 1);
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001113 if (!cp) {
1114 /* The packet could also belong to a local client */
1115 cp = pp->conn_out_get(AF_INET, skb, pp, &ciph, offset, 1);
Simon Hormanf2428ed2008-09-05 11:17:14 +10001116 if (cp) {
1117 snet.ip = iph->saddr;
1118 return handle_response_icmp(AF_INET, skb, &snet,
1119 cih->protocol, cp, pp,
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001120 offset, ihl);
Simon Hormanf2428ed2008-09-05 11:17:14 +10001121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return NF_ACCEPT;
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 verdict = NF_DROP;
1126
1127 /* Ensure the checksum is correct */
Herbert Xu60476372007-04-09 11:59:39 -07001128 if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* Failed checksum! */
Harvey Harrison14d5e832008-10-31 00:54:29 -07001130 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1131 &iph->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 goto out;
1133 }
1134
1135 /* do the statistics and put it back */
1136 ip_vs_in_stats(cp, skb);
1137 if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
1138 offset += 2 * sizeof(__u16);
1139 verdict = ip_vs_icmp_xmit(skb, cp, pp, offset);
1140 /* do not touch skb anymore */
1141
1142 out:
1143 __ip_vs_conn_put(cp);
1144
1145 return verdict;
1146}
1147
Julius Volz2a3b7912008-09-02 15:55:47 +02001148#ifdef CONFIG_IP_VS_IPV6
1149static int
1150ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
1151{
1152 struct ipv6hdr *iph;
1153 struct icmp6hdr _icmph, *ic;
1154 struct ipv6hdr _ciph, *cih; /* The ip header contained
1155 within the ICMP */
1156 struct ip_vs_iphdr ciph;
1157 struct ip_vs_conn *cp;
1158 struct ip_vs_protocol *pp;
1159 unsigned int offset, verdict;
Simon Hormanf2428ed2008-09-05 11:17:14 +10001160 union nf_inet_addr snet;
Julius Volz2a3b7912008-09-02 15:55:47 +02001161
1162 *related = 1;
1163
1164 /* reassemble IP fragments */
1165 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1166 if (ip_vs_gather_frags_v6(skb, hooknum == NF_INET_LOCAL_IN ?
1167 IP_DEFRAG_VS_IN :
1168 IP_DEFRAG_VS_FWD))
1169 return NF_STOLEN;
1170 }
1171
1172 iph = ipv6_hdr(skb);
1173 offset = sizeof(struct ipv6hdr);
1174 ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1175 if (ic == NULL)
1176 return NF_DROP;
1177
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001178 IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) %pI6->%pI6\n",
Julius Volz2a3b7912008-09-02 15:55:47 +02001179 ic->icmp6_type, ntohs(icmpv6_id(ic)),
Harvey Harrison38ff4fa2008-10-28 16:08:13 -07001180 &iph->saddr, &iph->daddr);
Julius Volz2a3b7912008-09-02 15:55:47 +02001181
1182 /*
1183 * Work through seeing if this is for us.
1184 * These checks are supposed to be in an order that means easy
1185 * things are checked first to speed up processing.... however
1186 * this means that some packets will manage to get a long way
1187 * down this stack and then be rejected, but that's life.
1188 */
1189 if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
1190 (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
1191 (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
1192 *related = 0;
1193 return NF_ACCEPT;
1194 }
1195
1196 /* Now find the contained IP header */
1197 offset += sizeof(_icmph);
1198 cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1199 if (cih == NULL)
1200 return NF_ACCEPT; /* The packet looks wrong, ignore */
1201
1202 pp = ip_vs_proto_get(cih->nexthdr);
1203 if (!pp)
1204 return NF_ACCEPT;
1205
1206 /* Is the embedded protocol header present? */
1207 /* TODO: we don't support fragmentation at the moment anyways */
1208 if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
1209 return NF_ACCEPT;
1210
1211 IP_VS_DBG_PKT(11, pp, skb, offset, "Checking incoming ICMPv6 for");
1212
1213 offset += sizeof(struct ipv6hdr);
1214
1215 ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
1216 /* The embedded headers contain source and dest in reverse order */
1217 cp = pp->conn_in_get(AF_INET6, skb, pp, &ciph, offset, 1);
Simon Hormanf2428ed2008-09-05 11:17:14 +10001218 if (!cp) {
1219 /* The packet could also belong to a local client */
1220 cp = pp->conn_out_get(AF_INET6, skb, pp, &ciph, offset, 1);
1221 if (cp) {
Simon Horman178f5e42008-09-08 09:34:46 +10001222 ipv6_addr_copy(&snet.in6, &iph->saddr);
Simon Hormanf2428ed2008-09-05 11:17:14 +10001223 return handle_response_icmp(AF_INET6, skb, &snet,
1224 cih->nexthdr,
1225 cp, pp, offset,
1226 sizeof(struct ipv6hdr));
1227 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001228 return NF_ACCEPT;
Simon Hormanf2428ed2008-09-05 11:17:14 +10001229 }
Julius Volz2a3b7912008-09-02 15:55:47 +02001230
1231 verdict = NF_DROP;
1232
1233 /* do the statistics and put it back */
1234 ip_vs_in_stats(cp, skb);
1235 if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr)
1236 offset += 2 * sizeof(__u16);
1237 verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset);
1238 /* do not touch skb anymore */
1239
1240 __ip_vs_conn_put(cp);
1241
1242 return verdict;
1243}
1244#endif
1245
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247/*
1248 * Check if it's for virtual services, look it up,
1249 * and send it on its way...
1250 */
1251static unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -07001252ip_vs_in(unsigned int hooknum, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 const struct net_device *in, const struct net_device *out,
1254 int (*okfn)(struct sk_buff *))
1255{
Julius Volz51ef3482008-09-02 15:55:40 +02001256 struct ip_vs_iphdr iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 struct ip_vs_protocol *pp;
1258 struct ip_vs_conn *cp;
Julius Volz2a3b7912008-09-02 15:55:47 +02001259 int ret, restart, af;
Julius Volz51ef3482008-09-02 15:55:40 +02001260
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001261 af = (skb->protocol == htons(ETH_P_IP)) ? AF_INET : AF_INET6;
Julius Volz2a3b7912008-09-02 15:55:47 +02001262
1263 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /*
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001266 * Big tappo: only PACKET_HOST, including loopback for local client
1267 * Don't handle local packets on IPv6 for now
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 */
Simon Hormanf2428ed2008-09-05 11:17:14 +10001269 if (unlikely(skb->pkt_type != PACKET_HOST)) {
Julius Volz51ef3482008-09-02 15:55:40 +02001270 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s ignored\n",
1271 skb->pkt_type,
1272 iph.protocol,
Julius Volz2a3b7912008-09-02 15:55:47 +02001273 IP_VS_DBG_ADDR(af, &iph.daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return NF_ACCEPT;
1275 }
1276
Julius Volz51ef3482008-09-02 15:55:40 +02001277 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
Herbert Xu3db05fe2007-10-15 00:53:15 -07001278 int related, verdict = ip_vs_in_icmp(skb, &related, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 if (related)
1281 return verdict;
Julius Volz2a3b7912008-09-02 15:55:47 +02001282 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284
1285 /* Protocol supported? */
Julius Volz51ef3482008-09-02 15:55:40 +02001286 pp = ip_vs_proto_get(iph.protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (unlikely(!pp))
1288 return NF_ACCEPT;
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 /*
1291 * Check if the packet belongs to an existing connection entry
1292 */
Julius Volz2a3b7912008-09-02 15:55:47 +02001293 cp = pp->conn_in_get(af, skb, pp, &iph, iph.len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 if (unlikely(!cp)) {
1296 int v;
1297
Malcolm Turnbull4856c842008-09-05 11:17:13 +10001298 /* For local client packets, it could be a response */
1299 cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
1300 if (cp)
1301 return handle_response(af, skb, pp, cp, iph.len);
1302
Julius Volz2a3b7912008-09-02 15:55:47 +02001303 if (!pp->conn_schedule(af, skb, pp, &v, &cp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return v;
1305 }
1306
1307 if (unlikely(!cp)) {
1308 /* sorry, all this trouble for a no-hit :) */
1309 IP_VS_DBG_PKT(12, pp, skb, 0,
1310 "packet continues traversal as normal");
1311 return NF_ACCEPT;
1312 }
1313
1314 IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
1315
1316 /* Check the server status */
1317 if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1318 /* the destination server is not available */
1319
1320 if (sysctl_ip_vs_expire_nodest_conn) {
1321 /* try to expire the connection immediately */
1322 ip_vs_conn_expire_now(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
Julian Anastasovdc8103f2005-11-08 09:40:05 -08001324 /* don't restart its timer, and silently
1325 drop the packet. */
1326 __ip_vs_conn_put(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return NF_DROP;
1328 }
1329
1330 ip_vs_in_stats(cp, skb);
1331 restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
1332 if (cp->packet_xmit)
1333 ret = cp->packet_xmit(skb, cp, pp);
1334 /* do not touch skb anymore */
1335 else {
1336 IP_VS_DBG_RL("warning: packet_xmit is null");
1337 ret = NF_ACCEPT;
1338 }
1339
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001340 /* Increase its packet counter and check if it is needed
1341 * to be synchronized
1342 *
1343 * Sync connection if it is about to close to
1344 * encorage the standby servers to update the connections timeout
1345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 atomic_inc(&cp->in_pkts);
Julius Volzc6883f52008-09-02 15:55:50 +02001347 if (af == AF_INET &&
1348 (ip_vs_sync_state & IP_VS_STATE_MASTER) &&
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001349 (((cp->protocol != IPPROTO_TCP ||
1350 cp->state == IP_VS_TCP_S_ESTABLISHED) &&
1351 (atomic_read(&cp->in_pkts) % sysctl_ip_vs_sync_threshold[1]
1352 == sysctl_ip_vs_sync_threshold[0])) ||
1353 ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
1354 ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
Rumen G. Bogdanovski9d3a0de2008-07-16 20:04:23 -07001355 (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
1356 (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 ip_vs_sync_conn(cp);
Rumen G. Bogdanovskiefac5272007-11-07 02:36:55 -08001358 cp->old_state = cp->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 ip_vs_conn_put(cp);
1361 return ret;
1362}
1363
1364
1365/*
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001366 * It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 * related packets destined for 0.0.0.0/0.
1368 * When fwmark-based virtual service is used, such as transparent
1369 * cache cluster, TCP packets can be marked and routed to ip_vs_in,
1370 * but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001371 * sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 * and send them to ip_vs_in_icmp.
1373 */
1374static unsigned int
Herbert Xu3db05fe2007-10-15 00:53:15 -07001375ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 const struct net_device *in, const struct net_device *out,
1377 int (*okfn)(struct sk_buff *))
1378{
1379 int r;
1380
Herbert Xu3db05fe2007-10-15 00:53:15 -07001381 if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return NF_ACCEPT;
1383
Herbert Xu3db05fe2007-10-15 00:53:15 -07001384 return ip_vs_in_icmp(skb, &r, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
Julius Volz2a3b7912008-09-02 15:55:47 +02001387#ifdef CONFIG_IP_VS_IPV6
1388static unsigned int
1389ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1390 const struct net_device *in, const struct net_device *out,
1391 int (*okfn)(struct sk_buff *))
1392{
1393 int r;
1394
1395 if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1396 return NF_ACCEPT;
1397
1398 return ip_vs_in_icmp_v6(skb, &r, hooknum);
1399}
1400#endif
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Patrick McHardy19994142007-12-05 01:23:00 -08001403static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
Patrick McHardy41c5b312007-12-05 01:22:43 -08001404 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1405 * or VS/NAT(change destination), so that filtering rules can be
1406 * applied to IPVS. */
1407 {
1408 .hook = ip_vs_in,
1409 .owner = THIS_MODULE,
1410 .pf = PF_INET,
1411 .hooknum = NF_INET_LOCAL_IN,
1412 .priority = 100,
1413 },
1414 /* After packet filtering, change source only for VS/NAT */
1415 {
1416 .hook = ip_vs_out,
1417 .owner = THIS_MODULE,
1418 .pf = PF_INET,
1419 .hooknum = NF_INET_FORWARD,
1420 .priority = 100,
1421 },
1422 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1423 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1424 {
1425 .hook = ip_vs_forward_icmp,
1426 .owner = THIS_MODULE,
1427 .pf = PF_INET,
1428 .hooknum = NF_INET_FORWARD,
1429 .priority = 99,
1430 },
1431 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1432 {
1433 .hook = ip_vs_post_routing,
1434 .owner = THIS_MODULE,
1435 .pf = PF_INET,
1436 .hooknum = NF_INET_POST_ROUTING,
1437 .priority = NF_IP_PRI_NAT_SRC-1,
1438 },
Julius Volz473b23d2008-09-02 15:55:54 +02001439#ifdef CONFIG_IP_VS_IPV6
1440 /* After packet filtering, forward packet through VS/DR, VS/TUN,
1441 * or VS/NAT(change destination), so that filtering rules can be
1442 * applied to IPVS. */
1443 {
1444 .hook = ip_vs_in,
1445 .owner = THIS_MODULE,
1446 .pf = PF_INET6,
1447 .hooknum = NF_INET_LOCAL_IN,
1448 .priority = 100,
1449 },
1450 /* After packet filtering, change source only for VS/NAT */
1451 {
1452 .hook = ip_vs_out,
1453 .owner = THIS_MODULE,
1454 .pf = PF_INET6,
1455 .hooknum = NF_INET_FORWARD,
1456 .priority = 100,
1457 },
1458 /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1459 * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1460 {
1461 .hook = ip_vs_forward_icmp_v6,
1462 .owner = THIS_MODULE,
1463 .pf = PF_INET6,
1464 .hooknum = NF_INET_FORWARD,
1465 .priority = 99,
1466 },
1467 /* Before the netfilter connection tracking, exit from POST_ROUTING */
1468 {
1469 .hook = ip_vs_post_routing,
1470 .owner = THIS_MODULE,
1471 .pf = PF_INET6,
1472 .hooknum = NF_INET_POST_ROUTING,
1473 .priority = NF_IP6_PRI_NAT_SRC-1,
1474 },
1475#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476};
1477
1478
1479/*
1480 * Initialize IP Virtual Server
1481 */
1482static int __init ip_vs_init(void)
1483{
1484 int ret;
1485
Sven Wegenera919cf42008-08-14 00:47:16 +02001486 ip_vs_estimator_init();
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 ret = ip_vs_control_init();
1489 if (ret < 0) {
1490 IP_VS_ERR("can't setup control.\n");
Sven Wegenera919cf42008-08-14 00:47:16 +02001491 goto cleanup_estimator;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493
1494 ip_vs_protocol_init();
1495
1496 ret = ip_vs_app_init();
1497 if (ret < 0) {
1498 IP_VS_ERR("can't setup application helper.\n");
1499 goto cleanup_protocol;
1500 }
1501
1502 ret = ip_vs_conn_init();
1503 if (ret < 0) {
1504 IP_VS_ERR("can't setup connection table.\n");
1505 goto cleanup_app;
1506 }
1507
Patrick McHardy41c5b312007-12-05 01:22:43 -08001508 ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (ret < 0) {
Patrick McHardy41c5b312007-12-05 01:22:43 -08001510 IP_VS_ERR("can't register hooks.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 goto cleanup_conn;
1512 }
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 IP_VS_INFO("ipvs loaded.\n");
1515 return ret;
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 cleanup_conn:
1518 ip_vs_conn_cleanup();
1519 cleanup_app:
1520 ip_vs_app_cleanup();
1521 cleanup_protocol:
1522 ip_vs_protocol_cleanup();
1523 ip_vs_control_cleanup();
Sven Wegenera919cf42008-08-14 00:47:16 +02001524 cleanup_estimator:
1525 ip_vs_estimator_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return ret;
1527}
1528
1529static void __exit ip_vs_cleanup(void)
1530{
Patrick McHardy41c5b312007-12-05 01:22:43 -08001531 nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 ip_vs_conn_cleanup();
1533 ip_vs_app_cleanup();
1534 ip_vs_protocol_cleanup();
1535 ip_vs_control_cleanup();
Sven Wegenera919cf42008-08-14 00:47:16 +02001536 ip_vs_estimator_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 IP_VS_INFO("ipvs unloaded.\n");
1538}
1539
1540module_init(ip_vs_init);
1541module_exit(ip_vs_cleanup);
1542MODULE_LICENSE("GPL");