pandora: defconfig: update
[pandora-kernel.git] / net / netfilter / ipvs / ip_vs_core.c
1 /*
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  *
8  * 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
23  *      Harald Welte                    don't use nfcache
24  *
25  */
26
27 #define KMSG_COMPONENT "IPVS"
28 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/ip.h>
33 #include <linux/tcp.h>
34 #include <linux/sctp.h>
35 #include <linux/icmp.h>
36 #include <linux/slab.h>
37
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>
43 #include <net/ip6_checksum.h>
44 #include <net/netns/generic.h>          /* net_generic() */
45
46 #include <linux/netfilter.h>
47 #include <linux/netfilter_ipv4.h>
48
49 #ifdef CONFIG_IP_VS_IPV6
50 #include <net/ipv6.h>
51 #include <linux/netfilter_ipv6.h>
52 #include <net/ip6_route.h>
53 #endif
54
55 #include <net/ip_vs.h>
56
57
58 EXPORT_SYMBOL(register_ip_vs_scheduler);
59 EXPORT_SYMBOL(unregister_ip_vs_scheduler);
60 EXPORT_SYMBOL(ip_vs_proto_name);
61 EXPORT_SYMBOL(ip_vs_conn_new);
62 EXPORT_SYMBOL(ip_vs_conn_in_get);
63 EXPORT_SYMBOL(ip_vs_conn_out_get);
64 #ifdef CONFIG_IP_VS_PROTO_TCP
65 EXPORT_SYMBOL(ip_vs_tcp_conn_listen);
66 #endif
67 EXPORT_SYMBOL(ip_vs_conn_put);
68 #ifdef CONFIG_IP_VS_DEBUG
69 EXPORT_SYMBOL(ip_vs_get_debug_level);
70 #endif
71
72 int ip_vs_net_id __read_mostly;
73 #ifdef IP_VS_GENERIC_NETNS
74 EXPORT_SYMBOL(ip_vs_net_id);
75 #endif
76 /* netns cnt used for uniqueness */
77 static atomic_t ipvs_netns_cnt = ATOMIC_INIT(0);
78
79 /* ID used in ICMP lookups */
80 #define icmp_id(icmph)          (((icmph)->un).echo.id)
81 #define icmpv6_id(icmph)        (icmph->icmp6_dataun.u_echo.identifier)
82
83 const char *ip_vs_proto_name(unsigned proto)
84 {
85         static char buf[20];
86
87         switch (proto) {
88         case IPPROTO_IP:
89                 return "IP";
90         case IPPROTO_UDP:
91                 return "UDP";
92         case IPPROTO_TCP:
93                 return "TCP";
94         case IPPROTO_SCTP:
95                 return "SCTP";
96         case IPPROTO_ICMP:
97                 return "ICMP";
98 #ifdef CONFIG_IP_VS_IPV6
99         case IPPROTO_ICMPV6:
100                 return "ICMPv6";
101 #endif
102         default:
103                 sprintf(buf, "IP_%d", proto);
104                 return buf;
105         }
106 }
107
108 void ip_vs_init_hash_table(struct list_head *table, int rows)
109 {
110         while (--rows >= 0)
111                 INIT_LIST_HEAD(&table[rows]);
112 }
113
114 static inline void
115 ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
116 {
117         struct ip_vs_dest *dest = cp->dest;
118         struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
119
120         if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
121                 struct ip_vs_cpu_stats *s;
122
123                 s = this_cpu_ptr(dest->stats.cpustats);
124                 s->ustats.inpkts++;
125                 u64_stats_update_begin(&s->syncp);
126                 s->ustats.inbytes += skb->len;
127                 u64_stats_update_end(&s->syncp);
128
129                 s = this_cpu_ptr(dest->svc->stats.cpustats);
130                 s->ustats.inpkts++;
131                 u64_stats_update_begin(&s->syncp);
132                 s->ustats.inbytes += skb->len;
133                 u64_stats_update_end(&s->syncp);
134
135                 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
136                 s->ustats.inpkts++;
137                 u64_stats_update_begin(&s->syncp);
138                 s->ustats.inbytes += skb->len;
139                 u64_stats_update_end(&s->syncp);
140         }
141 }
142
143
144 static inline void
145 ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
146 {
147         struct ip_vs_dest *dest = cp->dest;
148         struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
149
150         if (dest && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
151                 struct ip_vs_cpu_stats *s;
152
153                 s = this_cpu_ptr(dest->stats.cpustats);
154                 s->ustats.outpkts++;
155                 u64_stats_update_begin(&s->syncp);
156                 s->ustats.outbytes += skb->len;
157                 u64_stats_update_end(&s->syncp);
158
159                 s = this_cpu_ptr(dest->svc->stats.cpustats);
160                 s->ustats.outpkts++;
161                 u64_stats_update_begin(&s->syncp);
162                 s->ustats.outbytes += skb->len;
163                 u64_stats_update_end(&s->syncp);
164
165                 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
166                 s->ustats.outpkts++;
167                 u64_stats_update_begin(&s->syncp);
168                 s->ustats.outbytes += skb->len;
169                 u64_stats_update_end(&s->syncp);
170         }
171 }
172
173
174 static inline void
175 ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
176 {
177         struct netns_ipvs *ipvs = net_ipvs(svc->net);
178         struct ip_vs_cpu_stats *s;
179
180         s = this_cpu_ptr(cp->dest->stats.cpustats);
181         s->ustats.conns++;
182
183         s = this_cpu_ptr(svc->stats.cpustats);
184         s->ustats.conns++;
185
186         s = this_cpu_ptr(ipvs->tot_stats.cpustats);
187         s->ustats.conns++;
188 }
189
190
191 static inline void
192 ip_vs_set_state(struct ip_vs_conn *cp, int direction,
193                 const struct sk_buff *skb,
194                 struct ip_vs_proto_data *pd)
195 {
196         if (likely(pd->pp->state_transition))
197                 pd->pp->state_transition(cp, direction, skb, pd);
198 }
199
200 static inline int
201 ip_vs_conn_fill_param_persist(const struct ip_vs_service *svc,
202                               struct sk_buff *skb, int protocol,
203                               const union nf_inet_addr *caddr, __be16 cport,
204                               const union nf_inet_addr *vaddr, __be16 vport,
205                               struct ip_vs_conn_param *p)
206 {
207         ip_vs_conn_fill_param(svc->net, svc->af, protocol, caddr, cport, vaddr,
208                               vport, p);
209         p->pe = svc->pe;
210         if (p->pe && p->pe->fill_param)
211                 return p->pe->fill_param(p, skb);
212
213         return 0;
214 }
215
216 /*
217  *  IPVS persistent scheduling function
218  *  It creates a connection entry according to its template if exists,
219  *  or selects a server and creates a connection entry plus a template.
220  *  Locking: we are svc user (svc->refcnt), so we hold all dests too
221  *  Protocols supported: TCP, UDP
222  */
223 static struct ip_vs_conn *
224 ip_vs_sched_persist(struct ip_vs_service *svc,
225                     struct sk_buff *skb,
226                     __be16 src_port, __be16 dst_port, int *ignored)
227 {
228         struct ip_vs_conn *cp = NULL;
229         struct ip_vs_iphdr iph;
230         struct ip_vs_dest *dest;
231         struct ip_vs_conn *ct;
232         __be16 dport = 0;               /* destination port to forward */
233         unsigned int flags;
234         struct ip_vs_conn_param param;
235         const union nf_inet_addr fwmark = { .ip = htonl(svc->fwmark) };
236         union nf_inet_addr snet;        /* source network of the client,
237                                            after masking */
238
239         ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
240
241         /* Mask saddr with the netmask to adjust template granularity */
242 #ifdef CONFIG_IP_VS_IPV6
243         if (svc->af == AF_INET6)
244                 ipv6_addr_prefix(&snet.in6, &iph.saddr.in6, svc->netmask);
245         else
246 #endif
247                 snet.ip = iph.saddr.ip & svc->netmask;
248
249         IP_VS_DBG_BUF(6, "p-schedule: src %s:%u dest %s:%u "
250                       "mnet %s\n",
251                       IP_VS_DBG_ADDR(svc->af, &iph.saddr), ntohs(src_port),
252                       IP_VS_DBG_ADDR(svc->af, &iph.daddr), ntohs(dst_port),
253                       IP_VS_DBG_ADDR(svc->af, &snet));
254
255         /*
256          * As far as we know, FTP is a very complicated network protocol, and
257          * it uses control connection and data connections. For active FTP,
258          * FTP server initialize data connection to the client, its source port
259          * is often 20. For passive FTP, FTP server tells the clients the port
260          * that it passively listens to,  and the client issues the data
261          * connection. In the tunneling or direct routing mode, the load
262          * balancer is on the client-to-server half of connection, the port
263          * number is unknown to the load balancer. So, a conn template like
264          * <caddr, 0, vaddr, 0, daddr, 0> is created for persistent FTP
265          * service, and a template like <caddr, 0, vaddr, vport, daddr, dport>
266          * is created for other persistent services.
267          */
268         {
269                 int protocol = iph.protocol;
270                 const union nf_inet_addr *vaddr = &iph.daddr;
271                 __be16 vport = 0;
272
273                 if (dst_port == svc->port) {
274                         /* non-FTP template:
275                          * <protocol, caddr, 0, vaddr, vport, daddr, dport>
276                          * FTP template:
277                          * <protocol, caddr, 0, vaddr, 0, daddr, 0>
278                          */
279                         if (svc->port != FTPPORT)
280                                 vport = dst_port;
281                 } else {
282                         /* Note: persistent fwmark-based services and
283                          * persistent port zero service are handled here.
284                          * fwmark template:
285                          * <IPPROTO_IP,caddr,0,fwmark,0,daddr,0>
286                          * port zero template:
287                          * <protocol,caddr,0,vaddr,0,daddr,0>
288                          */
289                         if (svc->fwmark) {
290                                 protocol = IPPROTO_IP;
291                                 vaddr = &fwmark;
292                         }
293                 }
294                 /* return *ignored = -1 so NF_DROP can be used */
295                 if (ip_vs_conn_fill_param_persist(svc, skb, protocol, &snet, 0,
296                                                   vaddr, vport, &param) < 0) {
297                         *ignored = -1;
298                         return NULL;
299                 }
300         }
301
302         /* Check if a template already exists */
303         ct = ip_vs_ct_in_get(&param);
304         if (!ct || !ip_vs_check_template(ct)) {
305                 /*
306                  * No template found or the dest of the connection
307                  * template is not available.
308                  * return *ignored=0 i.e. ICMP and NF_DROP
309                  */
310                 dest = svc->scheduler->schedule(svc, skb);
311                 if (!dest) {
312                         IP_VS_DBG(1, "p-schedule: no dest found.\n");
313                         kfree(param.pe_data);
314                         *ignored = 0;
315                         return NULL;
316                 }
317
318                 if (dst_port == svc->port && svc->port != FTPPORT)
319                         dport = dest->port;
320
321                 /* Create a template
322                  * This adds param.pe_data to the template,
323                  * and thus param.pe_data will be destroyed
324                  * when the template expires */
325                 ct = ip_vs_conn_new(&param, &dest->addr, dport,
326                                     IP_VS_CONN_F_TEMPLATE, dest, skb->mark);
327                 if (ct == NULL) {
328                         kfree(param.pe_data);
329                         *ignored = -1;
330                         return NULL;
331                 }
332
333                 ct->timeout = svc->timeout;
334         } else {
335                 /* set destination with the found template */
336                 dest = ct->dest;
337                 kfree(param.pe_data);
338         }
339
340         dport = dst_port;
341         if (dport == svc->port && dest->port)
342                 dport = dest->port;
343
344         flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
345                  && iph.protocol == IPPROTO_UDP)?
346                 IP_VS_CONN_F_ONE_PACKET : 0;
347
348         /*
349          *    Create a new connection according to the template
350          */
351         ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol, &iph.saddr,
352                               src_port, &iph.daddr, dst_port, &param);
353
354         cp = ip_vs_conn_new(&param, &dest->addr, dport, flags, dest, skb->mark);
355         if (cp == NULL) {
356                 ip_vs_conn_put(ct);
357                 *ignored = -1;
358                 return NULL;
359         }
360
361         /*
362          *    Add its control
363          */
364         ip_vs_control_add(cp, ct);
365         ip_vs_conn_put(ct);
366
367         ip_vs_conn_stats(cp, svc);
368         return cp;
369 }
370
371
372 /*
373  *  IPVS main scheduling function
374  *  It selects a server according to the virtual service, and
375  *  creates a connection entry.
376  *  Protocols supported: TCP, UDP
377  *
378  *  Usage of *ignored
379  *
380  * 1 :   protocol tried to schedule (eg. on SYN), found svc but the
381  *       svc/scheduler decides that this packet should be accepted with
382  *       NF_ACCEPT because it must not be scheduled.
383  *
384  * 0 :   scheduler can not find destination, so try bypass or
385  *       return ICMP and then NF_DROP (ip_vs_leave).
386  *
387  * -1 :  scheduler tried to schedule but fatal error occurred, eg.
388  *       ip_vs_conn_new failure (ENOMEM) or ip_vs_sip_fill_param
389  *       failure such as missing Call-ID, ENOMEM on skb_linearize
390  *       or pe_data. In this case we should return NF_DROP without
391  *       any attempts to send ICMP with ip_vs_leave.
392  */
393 struct ip_vs_conn *
394 ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
395                struct ip_vs_proto_data *pd, int *ignored)
396 {
397         struct ip_vs_protocol *pp = pd->pp;
398         struct ip_vs_conn *cp = NULL;
399         struct ip_vs_iphdr iph;
400         struct ip_vs_dest *dest;
401         __be16 _ports[2], *pptr;
402         unsigned int flags;
403
404         *ignored = 1;
405         ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
406         pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
407         if (pptr == NULL)
408                 return NULL;
409
410         /*
411          * FTPDATA needs this check when using local real server.
412          * Never schedule Active FTPDATA connections from real server.
413          * For LVS-NAT they must be already created. For other methods
414          * with persistence the connection is created on SYN+ACK.
415          */
416         if (pptr[0] == FTPDATA) {
417                 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
418                               "Not scheduling FTPDATA");
419                 return NULL;
420         }
421
422         /*
423          *    Do not schedule replies from local real server.
424          */
425         if ((!skb->dev || skb->dev->flags & IFF_LOOPBACK) &&
426             (cp = pp->conn_in_get(svc->af, skb, &iph, iph.len, 1))) {
427                 IP_VS_DBG_PKT(12, svc->af, pp, skb, 0,
428                               "Not scheduling reply for existing connection");
429                 __ip_vs_conn_put(cp);
430                 return NULL;
431         }
432
433         /*
434          *    Persistent service
435          */
436         if (svc->flags & IP_VS_SVC_F_PERSISTENT)
437                 return ip_vs_sched_persist(svc, skb, pptr[0], pptr[1], ignored);
438
439         *ignored = 0;
440
441         /*
442          *    Non-persistent service
443          */
444         if (!svc->fwmark && pptr[1] != svc->port) {
445                 if (!svc->port)
446                         pr_err("Schedule: port zero only supported "
447                                "in persistent services, "
448                                "check your ipvs configuration\n");
449                 return NULL;
450         }
451
452         dest = svc->scheduler->schedule(svc, skb);
453         if (dest == NULL) {
454                 IP_VS_DBG(1, "Schedule: no dest found.\n");
455                 return NULL;
456         }
457
458         flags = (svc->flags & IP_VS_SVC_F_ONEPACKET
459                  && iph.protocol == IPPROTO_UDP)?
460                 IP_VS_CONN_F_ONE_PACKET : 0;
461
462         /*
463          *    Create a connection entry.
464          */
465         {
466                 struct ip_vs_conn_param p;
467
468                 ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol,
469                                       &iph.saddr, pptr[0], &iph.daddr, pptr[1],
470                                       &p);
471                 cp = ip_vs_conn_new(&p, &dest->addr,
472                                     dest->port ? dest->port : pptr[1],
473                                     flags, dest, skb->mark);
474                 if (!cp) {
475                         *ignored = -1;
476                         return NULL;
477                 }
478         }
479
480         IP_VS_DBG_BUF(6, "Schedule fwd:%c c:%s:%u v:%s:%u "
481                       "d:%s:%u conn->flags:%X conn->refcnt:%d\n",
482                       ip_vs_fwd_tag(cp),
483                       IP_VS_DBG_ADDR(svc->af, &cp->caddr), ntohs(cp->cport),
484                       IP_VS_DBG_ADDR(svc->af, &cp->vaddr), ntohs(cp->vport),
485                       IP_VS_DBG_ADDR(svc->af, &cp->daddr), ntohs(cp->dport),
486                       cp->flags, atomic_read(&cp->refcnt));
487
488         ip_vs_conn_stats(cp, svc);
489         return cp;
490 }
491
492
493 /*
494  *  Pass or drop the packet.
495  *  Called by ip_vs_in, when the virtual service is available but
496  *  no destination is available for a new connection.
497  */
498 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
499                 struct ip_vs_proto_data *pd)
500 {
501         __be16 _ports[2], *pptr;
502         struct ip_vs_iphdr iph;
503 #ifdef CONFIG_SYSCTL
504         struct net *net;
505         struct netns_ipvs *ipvs;
506         int unicast;
507 #endif
508
509         ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph);
510
511         pptr = skb_header_pointer(skb, iph.len, sizeof(_ports), _ports);
512         if (pptr == NULL) {
513                 ip_vs_service_put(svc);
514                 return NF_DROP;
515         }
516
517 #ifdef CONFIG_SYSCTL
518         net = skb_net(skb);
519
520 #ifdef CONFIG_IP_VS_IPV6
521         if (svc->af == AF_INET6)
522                 unicast = ipv6_addr_type(&iph.daddr.in6) & IPV6_ADDR_UNICAST;
523         else
524 #endif
525                 unicast = (inet_addr_type(net, iph.daddr.ip) == RTN_UNICAST);
526
527         /* if it is fwmark-based service, the cache_bypass sysctl is up
528            and the destination is a non-local unicast, then create
529            a cache_bypass connection entry */
530         ipvs = net_ipvs(net);
531         if (ipvs->sysctl_cache_bypass && svc->fwmark && unicast) {
532                 int ret;
533                 struct ip_vs_conn *cp;
534                 unsigned int flags = (svc->flags & IP_VS_SVC_F_ONEPACKET &&
535                                       iph.protocol == IPPROTO_UDP)?
536                                       IP_VS_CONN_F_ONE_PACKET : 0;
537                 union nf_inet_addr daddr =  { .all = { 0, 0, 0, 0 } };
538
539                 ip_vs_service_put(svc);
540
541                 /* create a new connection entry */
542                 IP_VS_DBG(6, "%s(): create a cache_bypass entry\n", __func__);
543                 {
544                         struct ip_vs_conn_param p;
545                         ip_vs_conn_fill_param(svc->net, svc->af, iph.protocol,
546                                               &iph.saddr, pptr[0],
547                                               &iph.daddr, pptr[1], &p);
548                         cp = ip_vs_conn_new(&p, &daddr, 0,
549                                             IP_VS_CONN_F_BYPASS | flags,
550                                             NULL, skb->mark);
551                         if (!cp)
552                                 return NF_DROP;
553                 }
554
555                 /* statistics */
556                 ip_vs_in_stats(cp, skb);
557
558                 /* set state */
559                 ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
560
561                 /* transmit the first SYN packet */
562                 ret = cp->packet_xmit(skb, cp, pd->pp);
563                 /* do not touch skb anymore */
564
565                 atomic_inc(&cp->in_pkts);
566                 ip_vs_conn_put(cp);
567                 return ret;
568         }
569 #endif
570
571         /*
572          * When the virtual ftp service is presented, packets destined
573          * for other services on the VIP may get here (except services
574          * listed in the ipvs table), pass the packets, because it is
575          * not ipvs job to decide to drop the packets.
576          */
577         if ((svc->port == FTPPORT) && (pptr[1] != FTPPORT)) {
578                 ip_vs_service_put(svc);
579                 return NF_ACCEPT;
580         }
581
582         ip_vs_service_put(svc);
583
584         /*
585          * Notify the client that the destination is unreachable, and
586          * release the socket buffer.
587          * Since it is in IP layer, the TCP socket is not actually
588          * created, the TCP RST packet cannot be sent, instead that
589          * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
590          */
591 #ifdef CONFIG_IP_VS_IPV6
592         if (svc->af == AF_INET6) {
593                 if (!skb->dev) {
594                         struct net *net = dev_net(skb_dst(skb)->dev);
595
596                         skb->dev = net->loopback_dev;
597                 }
598                 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
599         } else
600 #endif
601                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
602
603         return NF_DROP;
604 }
605
606 #ifdef CONFIG_SYSCTL
607
608 static int sysctl_snat_reroute(struct sk_buff *skb)
609 {
610         struct netns_ipvs *ipvs = net_ipvs(skb_net(skb));
611         return ipvs->sysctl_snat_reroute;
612 }
613
614 static int sysctl_nat_icmp_send(struct net *net)
615 {
616         struct netns_ipvs *ipvs = net_ipvs(net);
617         return ipvs->sysctl_nat_icmp_send;
618 }
619
620 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
621 {
622         return ipvs->sysctl_expire_nodest_conn;
623 }
624
625 #else
626
627 static int sysctl_snat_reroute(struct sk_buff *skb) { return 0; }
628 static int sysctl_nat_icmp_send(struct net *net) { return 0; }
629 static int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs) { return 0; }
630
631 #endif
632
633 __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset)
634 {
635         return csum_fold(skb_checksum(skb, offset, skb->len - offset, 0));
636 }
637
638 static inline enum ip_defrag_users ip_vs_defrag_user(unsigned int hooknum)
639 {
640         if (NF_INET_LOCAL_IN == hooknum)
641                 return IP_DEFRAG_VS_IN;
642         if (NF_INET_FORWARD == hooknum)
643                 return IP_DEFRAG_VS_FWD;
644         return IP_DEFRAG_VS_OUT;
645 }
646
647 static inline int ip_vs_gather_frags(struct sk_buff *skb, u_int32_t user)
648 {
649         int err = ip_defrag(skb, user);
650
651         if (!err)
652                 ip_send_check(ip_hdr(skb));
653
654         return err;
655 }
656
657 #ifdef CONFIG_IP_VS_IPV6
658 static inline int ip_vs_gather_frags_v6(struct sk_buff *skb, u_int32_t user)
659 {
660         /* TODO IPv6: Find out what to do here for IPv6 */
661         return 0;
662 }
663 #endif
664
665 static int ip_vs_route_me_harder(int af, struct sk_buff *skb,
666                                  unsigned int hooknum)
667 {
668         if (!sysctl_snat_reroute(skb))
669                 return 0;
670         /* Reroute replies only to remote clients (FORWARD and LOCAL_OUT) */
671         if (NF_INET_LOCAL_IN == hooknum)
672                 return 0;
673 #ifdef CONFIG_IP_VS_IPV6
674         if (af == AF_INET6) {
675                 struct dst_entry *dst = skb_dst(skb);
676
677                 if (dst->dev && !(dst->dev->flags & IFF_LOOPBACK) &&
678                     ip6_route_me_harder(skb) != 0)
679                         return 1;
680         } else
681 #endif
682                 if (!(skb_rtable(skb)->rt_flags & RTCF_LOCAL) &&
683                     ip_route_me_harder(skb, RTN_LOCAL) != 0)
684                         return 1;
685
686         return 0;
687 }
688
689 /*
690  * Packet has been made sufficiently writable in caller
691  * - inout: 1=in->out, 0=out->in
692  */
693 void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
694                     struct ip_vs_conn *cp, int inout)
695 {
696         struct iphdr *iph        = ip_hdr(skb);
697         unsigned int icmp_offset = iph->ihl*4;
698         struct icmphdr *icmph    = (struct icmphdr *)(skb_network_header(skb) +
699                                                       icmp_offset);
700         struct iphdr *ciph       = (struct iphdr *)(icmph + 1);
701
702         if (inout) {
703                 iph->saddr = cp->vaddr.ip;
704                 ip_send_check(iph);
705                 ciph->daddr = cp->vaddr.ip;
706                 ip_send_check(ciph);
707         } else {
708                 iph->daddr = cp->daddr.ip;
709                 ip_send_check(iph);
710                 ciph->saddr = cp->daddr.ip;
711                 ip_send_check(ciph);
712         }
713
714         /* the TCP/UDP/SCTP port */
715         if (IPPROTO_TCP == ciph->protocol || IPPROTO_UDP == ciph->protocol ||
716             IPPROTO_SCTP == ciph->protocol) {
717                 __be16 *ports = (void *)ciph + ciph->ihl*4;
718
719                 if (inout)
720                         ports[1] = cp->vport;
721                 else
722                         ports[0] = cp->dport;
723         }
724
725         /* And finally the ICMP checksum */
726         icmph->checksum = 0;
727         icmph->checksum = ip_vs_checksum_complete(skb, icmp_offset);
728         skb->ip_summed = CHECKSUM_UNNECESSARY;
729
730         if (inout)
731                 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
732                         "Forwarding altered outgoing ICMP");
733         else
734                 IP_VS_DBG_PKT(11, AF_INET, pp, skb, (void *)ciph - (void *)iph,
735                         "Forwarding altered incoming ICMP");
736 }
737
738 #ifdef CONFIG_IP_VS_IPV6
739 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
740                     struct ip_vs_conn *cp, int inout)
741 {
742         struct ipv6hdr *iph      = ipv6_hdr(skb);
743         unsigned int icmp_offset = sizeof(struct ipv6hdr);
744         struct icmp6hdr *icmph   = (struct icmp6hdr *)(skb_network_header(skb) +
745                                                       icmp_offset);
746         struct ipv6hdr *ciph     = (struct ipv6hdr *)(icmph + 1);
747
748         if (inout) {
749                 iph->saddr = cp->vaddr.in6;
750                 ciph->daddr = cp->vaddr.in6;
751         } else {
752                 iph->daddr = cp->daddr.in6;
753                 ciph->saddr = cp->daddr.in6;
754         }
755
756         /* the TCP/UDP/SCTP port */
757         if (IPPROTO_TCP == ciph->nexthdr || IPPROTO_UDP == ciph->nexthdr ||
758             IPPROTO_SCTP == ciph->nexthdr) {
759                 __be16 *ports = (void *)ciph + sizeof(struct ipv6hdr);
760
761                 if (inout)
762                         ports[1] = cp->vport;
763                 else
764                         ports[0] = cp->dport;
765         }
766
767         /* And finally the ICMP checksum */
768         icmph->icmp6_cksum = ~csum_ipv6_magic(&iph->saddr, &iph->daddr,
769                                               skb->len - icmp_offset,
770                                               IPPROTO_ICMPV6, 0);
771         skb->csum_start = skb_network_header(skb) - skb->head + icmp_offset;
772         skb->csum_offset = offsetof(struct icmp6hdr, icmp6_cksum);
773         skb->ip_summed = CHECKSUM_PARTIAL;
774
775         if (inout)
776                 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
777                               (void *)ciph - (void *)iph,
778                               "Forwarding altered outgoing ICMPv6");
779         else
780                 IP_VS_DBG_PKT(11, AF_INET6, pp, skb,
781                               (void *)ciph - (void *)iph,
782                               "Forwarding altered incoming ICMPv6");
783 }
784 #endif
785
786 /* Handle relevant response ICMP messages - forward to the right
787  * destination host.
788  */
789 static int handle_response_icmp(int af, struct sk_buff *skb,
790                                 union nf_inet_addr *snet,
791                                 __u8 protocol, struct ip_vs_conn *cp,
792                                 struct ip_vs_protocol *pp,
793                                 unsigned int offset, unsigned int ihl,
794                                 unsigned int hooknum)
795 {
796         unsigned int verdict = NF_DROP;
797
798         if (IP_VS_FWD_METHOD(cp) != 0) {
799                 pr_err("shouldn't reach here, because the box is on the "
800                        "half connection in the tun/dr module.\n");
801         }
802
803         /* Ensure the checksum is correct */
804         if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
805                 /* Failed checksum! */
806                 IP_VS_DBG_BUF(1, "Forward ICMP: failed checksum from %s!\n",
807                               IP_VS_DBG_ADDR(af, snet));
808                 goto out;
809         }
810
811         if (IPPROTO_TCP == protocol || IPPROTO_UDP == protocol ||
812             IPPROTO_SCTP == protocol)
813                 offset += 2 * sizeof(__u16);
814         if (!skb_make_writable(skb, offset))
815                 goto out;
816
817 #ifdef CONFIG_IP_VS_IPV6
818         if (af == AF_INET6)
819                 ip_vs_nat_icmp_v6(skb, pp, cp, 1);
820         else
821 #endif
822                 ip_vs_nat_icmp(skb, pp, cp, 1);
823
824         if (ip_vs_route_me_harder(af, skb, hooknum))
825                 goto out;
826
827         /* do the statistics and put it back */
828         ip_vs_out_stats(cp, skb);
829
830         skb->ipvs_property = 1;
831         if (!(cp->flags & IP_VS_CONN_F_NFCT))
832                 ip_vs_notrack(skb);
833         else
834                 ip_vs_update_conntrack(skb, cp, 0);
835         verdict = NF_ACCEPT;
836
837 out:
838         __ip_vs_conn_put(cp);
839
840         return verdict;
841 }
842
843 /*
844  *      Handle ICMP messages in the inside-to-outside direction (outgoing).
845  *      Find any that might be relevant, check against existing connections.
846  *      Currently handles error types - unreachable, quench, ttl exceeded.
847  */
848 static int ip_vs_out_icmp(struct sk_buff *skb, int *related,
849                           unsigned int hooknum)
850 {
851         struct iphdr *iph;
852         struct icmphdr  _icmph, *ic;
853         struct iphdr    _ciph, *cih;    /* The ip header contained within the ICMP */
854         struct ip_vs_iphdr ciph;
855         struct ip_vs_conn *cp;
856         struct ip_vs_protocol *pp;
857         unsigned int offset, ihl;
858         union nf_inet_addr snet;
859
860         *related = 1;
861
862         /* reassemble IP fragments */
863         if (ip_is_fragment(ip_hdr(skb))) {
864                 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
865                         return NF_STOLEN;
866         }
867
868         iph = ip_hdr(skb);
869         offset = ihl = iph->ihl * 4;
870         ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
871         if (ic == NULL)
872                 return NF_DROP;
873
874         IP_VS_DBG(12, "Outgoing ICMP (%d,%d) %pI4->%pI4\n",
875                   ic->type, ntohs(icmp_id(ic)),
876                   &iph->saddr, &iph->daddr);
877
878         /*
879          * Work through seeing if this is for us.
880          * These checks are supposed to be in an order that means easy
881          * things are checked first to speed up processing.... however
882          * this means that some packets will manage to get a long way
883          * down this stack and then be rejected, but that's life.
884          */
885         if ((ic->type != ICMP_DEST_UNREACH) &&
886             (ic->type != ICMP_SOURCE_QUENCH) &&
887             (ic->type != ICMP_TIME_EXCEEDED)) {
888                 *related = 0;
889                 return NF_ACCEPT;
890         }
891
892         /* Now find the contained IP header */
893         offset += sizeof(_icmph);
894         cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
895         if (cih == NULL)
896                 return NF_ACCEPT; /* The packet looks wrong, ignore */
897
898         pp = ip_vs_proto_get(cih->protocol);
899         if (!pp)
900                 return NF_ACCEPT;
901
902         /* Is the embedded protocol header present? */
903         if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
904                      pp->dont_defrag))
905                 return NF_ACCEPT;
906
907         IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
908                       "Checking outgoing ICMP for");
909
910         offset += cih->ihl * 4;
911
912         ip_vs_fill_iphdr(AF_INET, cih, &ciph);
913         /* The embedded headers contain source and dest in reverse order */
914         cp = pp->conn_out_get(AF_INET, skb, &ciph, offset, 1);
915         if (!cp)
916                 return NF_ACCEPT;
917
918         snet.ip = iph->saddr;
919         return handle_response_icmp(AF_INET, skb, &snet, cih->protocol, cp,
920                                     pp, offset, ihl, hooknum);
921 }
922
923 #ifdef CONFIG_IP_VS_IPV6
924 static int ip_vs_out_icmp_v6(struct sk_buff *skb, int *related,
925                              unsigned int hooknum)
926 {
927         struct ipv6hdr *iph;
928         struct icmp6hdr _icmph, *ic;
929         struct ipv6hdr  _ciph, *cih;    /* The ip header contained
930                                            within the ICMP */
931         struct ip_vs_iphdr ciph;
932         struct ip_vs_conn *cp;
933         struct ip_vs_protocol *pp;
934         unsigned int offset;
935         union nf_inet_addr snet;
936
937         *related = 1;
938
939         /* reassemble IP fragments */
940         if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
941                 if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum)))
942                         return NF_STOLEN;
943         }
944
945         iph = ipv6_hdr(skb);
946         offset = sizeof(struct ipv6hdr);
947         ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
948         if (ic == NULL)
949                 return NF_DROP;
950
951         IP_VS_DBG(12, "Outgoing ICMPv6 (%d,%d) %pI6->%pI6\n",
952                   ic->icmp6_type, ntohs(icmpv6_id(ic)),
953                   &iph->saddr, &iph->daddr);
954
955         /*
956          * Work through seeing if this is for us.
957          * These checks are supposed to be in an order that means easy
958          * things are checked first to speed up processing.... however
959          * this means that some packets will manage to get a long way
960          * down this stack and then be rejected, but that's life.
961          */
962         if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
963             (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
964             (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
965                 *related = 0;
966                 return NF_ACCEPT;
967         }
968
969         /* Now find the contained IP header */
970         offset += sizeof(_icmph);
971         cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
972         if (cih == NULL)
973                 return NF_ACCEPT; /* The packet looks wrong, ignore */
974
975         pp = ip_vs_proto_get(cih->nexthdr);
976         if (!pp)
977                 return NF_ACCEPT;
978
979         /* Is the embedded protocol header present? */
980         /* TODO: we don't support fragmentation at the moment anyways */
981         if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
982                 return NF_ACCEPT;
983
984         IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
985                       "Checking outgoing ICMPv6 for");
986
987         offset += sizeof(struct ipv6hdr);
988
989         ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
990         /* The embedded headers contain source and dest in reverse order */
991         cp = pp->conn_out_get(AF_INET6, skb, &ciph, offset, 1);
992         if (!cp)
993                 return NF_ACCEPT;
994
995         ipv6_addr_copy(&snet.in6, &iph->saddr);
996         return handle_response_icmp(AF_INET6, skb, &snet, cih->nexthdr, cp,
997                                     pp, offset, sizeof(struct ipv6hdr),
998                                     hooknum);
999 }
1000 #endif
1001
1002 /*
1003  * Check if sctp chunc is ABORT chunk
1004  */
1005 static inline int is_sctp_abort(const struct sk_buff *skb, int nh_len)
1006 {
1007         sctp_chunkhdr_t *sch, schunk;
1008         sch = skb_header_pointer(skb, nh_len + sizeof(sctp_sctphdr_t),
1009                         sizeof(schunk), &schunk);
1010         if (sch == NULL)
1011                 return 0;
1012         if (sch->type == SCTP_CID_ABORT)
1013                 return 1;
1014         return 0;
1015 }
1016
1017 static inline int is_tcp_reset(const struct sk_buff *skb, int nh_len)
1018 {
1019         struct tcphdr _tcph, *th;
1020
1021         th = skb_header_pointer(skb, nh_len, sizeof(_tcph), &_tcph);
1022         if (th == NULL)
1023                 return 0;
1024         return th->rst;
1025 }
1026
1027 /* Handle response packets: rewrite addresses and send away...
1028  */
1029 static unsigned int
1030 handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
1031                 struct ip_vs_conn *cp, int ihl, unsigned int hooknum)
1032 {
1033         struct ip_vs_protocol *pp = pd->pp;
1034
1035         IP_VS_DBG_PKT(11, af, pp, skb, 0, "Outgoing packet");
1036
1037         if (!skb_make_writable(skb, ihl))
1038                 goto drop;
1039
1040         /* mangle the packet */
1041         if (pp->snat_handler && !pp->snat_handler(skb, pp, cp))
1042                 goto drop;
1043
1044 #ifdef CONFIG_IP_VS_IPV6
1045         if (af == AF_INET6)
1046                 ipv6_hdr(skb)->saddr = cp->vaddr.in6;
1047         else
1048 #endif
1049         {
1050                 ip_hdr(skb)->saddr = cp->vaddr.ip;
1051                 ip_send_check(ip_hdr(skb));
1052         }
1053
1054         /*
1055          * nf_iterate does not expect change in the skb->dst->dev.
1056          * It looks like it is not fatal to enable this code for hooks
1057          * where our handlers are at the end of the chain list and
1058          * when all next handlers use skb->dst->dev and not outdev.
1059          * It will definitely route properly the inout NAT traffic
1060          * when multiple paths are used.
1061          */
1062
1063         /* For policy routing, packets originating from this
1064          * machine itself may be routed differently to packets
1065          * passing through.  We want this packet to be routed as
1066          * if it came from this machine itself.  So re-compute
1067          * the routing information.
1068          */
1069         if (ip_vs_route_me_harder(af, skb, hooknum))
1070                 goto drop;
1071
1072         IP_VS_DBG_PKT(10, af, pp, skb, 0, "After SNAT");
1073
1074         ip_vs_out_stats(cp, skb);
1075         ip_vs_set_state(cp, IP_VS_DIR_OUTPUT, skb, pd);
1076         skb->ipvs_property = 1;
1077         if (!(cp->flags & IP_VS_CONN_F_NFCT))
1078                 ip_vs_notrack(skb);
1079         else
1080                 ip_vs_update_conntrack(skb, cp, 0);
1081         ip_vs_conn_put(cp);
1082
1083         LeaveFunction(11);
1084         return NF_ACCEPT;
1085
1086 drop:
1087         ip_vs_conn_put(cp);
1088         kfree_skb(skb);
1089         LeaveFunction(11);
1090         return NF_STOLEN;
1091 }
1092
1093 /*
1094  *      Check if outgoing packet belongs to the established ip_vs_conn.
1095  */
1096 static unsigned int
1097 ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
1098 {
1099         struct net *net = NULL;
1100         struct ip_vs_iphdr iph;
1101         struct ip_vs_protocol *pp;
1102         struct ip_vs_proto_data *pd;
1103         struct ip_vs_conn *cp;
1104
1105         EnterFunction(11);
1106
1107         /* Already marked as IPVS request or reply? */
1108         if (skb->ipvs_property)
1109                 return NF_ACCEPT;
1110
1111         /* Bad... Do not break raw sockets */
1112         if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1113                      af == AF_INET)) {
1114                 struct sock *sk = skb->sk;
1115                 struct inet_sock *inet = inet_sk(skb->sk);
1116
1117                 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1118                         return NF_ACCEPT;
1119         }
1120
1121         if (unlikely(!skb_dst(skb)))
1122                 return NF_ACCEPT;
1123
1124         net = skb_net(skb);
1125         if (!net_ipvs(net)->enable)
1126                 return NF_ACCEPT;
1127
1128         ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1129 #ifdef CONFIG_IP_VS_IPV6
1130         if (af == AF_INET6) {
1131                 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1132                         int related;
1133                         int verdict = ip_vs_out_icmp_v6(skb, &related,
1134                                                         hooknum);
1135
1136                         if (related)
1137                                 return verdict;
1138                         ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1139                 }
1140         } else
1141 #endif
1142                 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1143                         int related;
1144                         int verdict = ip_vs_out_icmp(skb, &related, hooknum);
1145
1146                         if (related)
1147                                 return verdict;
1148                         ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1149                 }
1150
1151         pd = ip_vs_proto_data_get(net, iph.protocol);
1152         if (unlikely(!pd))
1153                 return NF_ACCEPT;
1154         pp = pd->pp;
1155
1156         /* reassemble IP fragments */
1157 #ifdef CONFIG_IP_VS_IPV6
1158         if (af == AF_INET6) {
1159                 if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1160                         if (ip_vs_gather_frags_v6(skb,
1161                                                   ip_vs_defrag_user(hooknum)))
1162                                 return NF_STOLEN;
1163                 }
1164
1165                 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1166         } else
1167 #endif
1168                 if (unlikely(ip_is_fragment(ip_hdr(skb)) && !pp->dont_defrag)) {
1169                         if (ip_vs_gather_frags(skb,
1170                                                ip_vs_defrag_user(hooknum)))
1171                                 return NF_STOLEN;
1172
1173                         ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1174                 }
1175
1176         /*
1177          * Check if the packet belongs to an existing entry
1178          */
1179         cp = pp->conn_out_get(af, skb, &iph, iph.len, 0);
1180
1181         if (likely(cp))
1182                 return handle_response(af, skb, pd, cp, iph.len, hooknum);
1183         if (sysctl_nat_icmp_send(net) &&
1184             (pp->protocol == IPPROTO_TCP ||
1185              pp->protocol == IPPROTO_UDP ||
1186              pp->protocol == IPPROTO_SCTP)) {
1187                 __be16 _ports[2], *pptr;
1188
1189                 pptr = skb_header_pointer(skb, iph.len,
1190                                           sizeof(_ports), _ports);
1191                 if (pptr == NULL)
1192                         return NF_ACCEPT;       /* Not for me */
1193                 if (ip_vs_lookup_real_service(net, af, iph.protocol,
1194                                               &iph.saddr,
1195                                               pptr[0])) {
1196                         /*
1197                          * Notify the real server: there is no
1198                          * existing entry if it is not RST
1199                          * packet or not TCP packet.
1200                          */
1201                         if ((iph.protocol != IPPROTO_TCP &&
1202                              iph.protocol != IPPROTO_SCTP)
1203                              || ((iph.protocol == IPPROTO_TCP
1204                                   && !is_tcp_reset(skb, iph.len))
1205                                  || (iph.protocol == IPPROTO_SCTP
1206                                         && !is_sctp_abort(skb,
1207                                                 iph.len)))) {
1208 #ifdef CONFIG_IP_VS_IPV6
1209                                 if (af == AF_INET6) {
1210                                         struct net *net =
1211                                                 dev_net(skb_dst(skb)->dev);
1212
1213                                         if (!skb->dev)
1214                                                 skb->dev = net->loopback_dev;
1215                                         icmpv6_send(skb,
1216                                                     ICMPV6_DEST_UNREACH,
1217                                                     ICMPV6_PORT_UNREACH,
1218                                                     0);
1219                                 } else
1220 #endif
1221                                         icmp_send(skb,
1222                                                   ICMP_DEST_UNREACH,
1223                                                   ICMP_PORT_UNREACH, 0);
1224                                 return NF_DROP;
1225                         }
1226                 }
1227         }
1228         IP_VS_DBG_PKT(12, af, pp, skb, 0,
1229                       "ip_vs_out: packet continues traversal as normal");
1230         return NF_ACCEPT;
1231 }
1232
1233 /*
1234  *      It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1235  *      used only for VS/NAT.
1236  *      Check if packet is reply for established ip_vs_conn.
1237  */
1238 static unsigned int
1239 ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb,
1240              const struct net_device *in, const struct net_device *out,
1241              int (*okfn)(struct sk_buff *))
1242 {
1243         return ip_vs_out(hooknum, skb, AF_INET);
1244 }
1245
1246 /*
1247  *      It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1248  *      Check if packet is reply for established ip_vs_conn.
1249  */
1250 static unsigned int
1251 ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb,
1252                    const struct net_device *in, const struct net_device *out,
1253                    int (*okfn)(struct sk_buff *))
1254 {
1255         unsigned int verdict;
1256
1257         /* Disable BH in LOCAL_OUT until all places are fixed */
1258         local_bh_disable();
1259         verdict = ip_vs_out(hooknum, skb, AF_INET);
1260         local_bh_enable();
1261         return verdict;
1262 }
1263
1264 #ifdef CONFIG_IP_VS_IPV6
1265
1266 /*
1267  *      It is hooked at the NF_INET_FORWARD and NF_INET_LOCAL_IN chain,
1268  *      used only for VS/NAT.
1269  *      Check if packet is reply for established ip_vs_conn.
1270  */
1271 static unsigned int
1272 ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb,
1273              const struct net_device *in, const struct net_device *out,
1274              int (*okfn)(struct sk_buff *))
1275 {
1276         return ip_vs_out(hooknum, skb, AF_INET6);
1277 }
1278
1279 /*
1280  *      It is hooked at the NF_INET_LOCAL_OUT chain, used only for VS/NAT.
1281  *      Check if packet is reply for established ip_vs_conn.
1282  */
1283 static unsigned int
1284 ip_vs_local_reply6(unsigned int hooknum, struct sk_buff *skb,
1285                    const struct net_device *in, const struct net_device *out,
1286                    int (*okfn)(struct sk_buff *))
1287 {
1288         unsigned int verdict;
1289
1290         /* Disable BH in LOCAL_OUT until all places are fixed */
1291         local_bh_disable();
1292         verdict = ip_vs_out(hooknum, skb, AF_INET6);
1293         local_bh_enable();
1294         return verdict;
1295 }
1296
1297 #endif
1298
1299 /*
1300  *      Handle ICMP messages in the outside-to-inside direction (incoming).
1301  *      Find any that might be relevant, check against existing connections,
1302  *      forward to the right destination host if relevant.
1303  *      Currently handles error types - unreachable, quench, ttl exceeded.
1304  */
1305 static int
1306 ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum)
1307 {
1308         struct net *net = NULL;
1309         struct iphdr *iph;
1310         struct icmphdr  _icmph, *ic;
1311         struct iphdr    _ciph, *cih;    /* The ip header contained within the ICMP */
1312         struct ip_vs_iphdr ciph;
1313         struct ip_vs_conn *cp;
1314         struct ip_vs_protocol *pp;
1315         struct ip_vs_proto_data *pd;
1316         unsigned int offset, ihl, verdict;
1317
1318         *related = 1;
1319
1320         /* reassemble IP fragments */
1321         if (ip_is_fragment(ip_hdr(skb))) {
1322                 if (ip_vs_gather_frags(skb, ip_vs_defrag_user(hooknum)))
1323                         return NF_STOLEN;
1324         }
1325
1326         iph = ip_hdr(skb);
1327         offset = ihl = iph->ihl * 4;
1328         ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1329         if (ic == NULL)
1330                 return NF_DROP;
1331
1332         IP_VS_DBG(12, "Incoming ICMP (%d,%d) %pI4->%pI4\n",
1333                   ic->type, ntohs(icmp_id(ic)),
1334                   &iph->saddr, &iph->daddr);
1335
1336         /*
1337          * Work through seeing if this is for us.
1338          * These checks are supposed to be in an order that means easy
1339          * things are checked first to speed up processing.... however
1340          * this means that some packets will manage to get a long way
1341          * down this stack and then be rejected, but that's life.
1342          */
1343         if ((ic->type != ICMP_DEST_UNREACH) &&
1344             (ic->type != ICMP_SOURCE_QUENCH) &&
1345             (ic->type != ICMP_TIME_EXCEEDED)) {
1346                 *related = 0;
1347                 return NF_ACCEPT;
1348         }
1349
1350         /* Now find the contained IP header */
1351         offset += sizeof(_icmph);
1352         cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1353         if (cih == NULL)
1354                 return NF_ACCEPT; /* The packet looks wrong, ignore */
1355
1356         net = skb_net(skb);
1357
1358         pd = ip_vs_proto_data_get(net, cih->protocol);
1359         if (!pd)
1360                 return NF_ACCEPT;
1361         pp = pd->pp;
1362
1363         /* Is the embedded protocol header present? */
1364         if (unlikely(cih->frag_off & htons(IP_OFFSET) &&
1365                      pp->dont_defrag))
1366                 return NF_ACCEPT;
1367
1368         IP_VS_DBG_PKT(11, AF_INET, pp, skb, offset,
1369                       "Checking incoming ICMP for");
1370
1371         offset += cih->ihl * 4;
1372
1373         ip_vs_fill_iphdr(AF_INET, cih, &ciph);
1374         /* The embedded headers contain source and dest in reverse order */
1375         cp = pp->conn_in_get(AF_INET, skb, &ciph, offset, 1);
1376         if (!cp)
1377                 return NF_ACCEPT;
1378
1379         verdict = NF_DROP;
1380
1381         /* Ensure the checksum is correct */
1382         if (!skb_csum_unnecessary(skb) && ip_vs_checksum_complete(skb, ihl)) {
1383                 /* Failed checksum! */
1384                 IP_VS_DBG(1, "Incoming ICMP: failed checksum from %pI4!\n",
1385                           &iph->saddr);
1386                 goto out;
1387         }
1388
1389         /* do the statistics and put it back */
1390         ip_vs_in_stats(cp, skb);
1391         if (IPPROTO_TCP == cih->protocol || IPPROTO_UDP == cih->protocol)
1392                 offset += 2 * sizeof(__u16);
1393         verdict = ip_vs_icmp_xmit(skb, cp, pp, offset, hooknum);
1394
1395 out:
1396         __ip_vs_conn_put(cp);
1397
1398         return verdict;
1399 }
1400
1401 #ifdef CONFIG_IP_VS_IPV6
1402 static int
1403 ip_vs_in_icmp_v6(struct sk_buff *skb, int *related, unsigned int hooknum)
1404 {
1405         struct net *net = NULL;
1406         struct ipv6hdr *iph;
1407         struct icmp6hdr _icmph, *ic;
1408         struct ipv6hdr  _ciph, *cih;    /* The ip header contained
1409                                            within the ICMP */
1410         struct ip_vs_iphdr ciph;
1411         struct ip_vs_conn *cp;
1412         struct ip_vs_protocol *pp;
1413         struct ip_vs_proto_data *pd;
1414         unsigned int offset, verdict;
1415
1416         *related = 1;
1417
1418         /* reassemble IP fragments */
1419         if (ipv6_hdr(skb)->nexthdr == IPPROTO_FRAGMENT) {
1420                 if (ip_vs_gather_frags_v6(skb, ip_vs_defrag_user(hooknum)))
1421                         return NF_STOLEN;
1422         }
1423
1424         iph = ipv6_hdr(skb);
1425         offset = sizeof(struct ipv6hdr);
1426         ic = skb_header_pointer(skb, offset, sizeof(_icmph), &_icmph);
1427         if (ic == NULL)
1428                 return NF_DROP;
1429
1430         IP_VS_DBG(12, "Incoming ICMPv6 (%d,%d) %pI6->%pI6\n",
1431                   ic->icmp6_type, ntohs(icmpv6_id(ic)),
1432                   &iph->saddr, &iph->daddr);
1433
1434         /*
1435          * Work through seeing if this is for us.
1436          * These checks are supposed to be in an order that means easy
1437          * things are checked first to speed up processing.... however
1438          * this means that some packets will manage to get a long way
1439          * down this stack and then be rejected, but that's life.
1440          */
1441         if ((ic->icmp6_type != ICMPV6_DEST_UNREACH) &&
1442             (ic->icmp6_type != ICMPV6_PKT_TOOBIG) &&
1443             (ic->icmp6_type != ICMPV6_TIME_EXCEED)) {
1444                 *related = 0;
1445                 return NF_ACCEPT;
1446         }
1447
1448         /* Now find the contained IP header */
1449         offset += sizeof(_icmph);
1450         cih = skb_header_pointer(skb, offset, sizeof(_ciph), &_ciph);
1451         if (cih == NULL)
1452                 return NF_ACCEPT; /* The packet looks wrong, ignore */
1453
1454         net = skb_net(skb);
1455         pd = ip_vs_proto_data_get(net, cih->nexthdr);
1456         if (!pd)
1457                 return NF_ACCEPT;
1458         pp = pd->pp;
1459
1460         /* Is the embedded protocol header present? */
1461         /* TODO: we don't support fragmentation at the moment anyways */
1462         if (unlikely(cih->nexthdr == IPPROTO_FRAGMENT && pp->dont_defrag))
1463                 return NF_ACCEPT;
1464
1465         IP_VS_DBG_PKT(11, AF_INET6, pp, skb, offset,
1466                       "Checking incoming ICMPv6 for");
1467
1468         offset += sizeof(struct ipv6hdr);
1469
1470         ip_vs_fill_iphdr(AF_INET6, cih, &ciph);
1471         /* The embedded headers contain source and dest in reverse order */
1472         cp = pp->conn_in_get(AF_INET6, skb, &ciph, offset, 1);
1473         if (!cp)
1474                 return NF_ACCEPT;
1475
1476         /* do the statistics and put it back */
1477         ip_vs_in_stats(cp, skb);
1478         if (IPPROTO_TCP == cih->nexthdr || IPPROTO_UDP == cih->nexthdr ||
1479             IPPROTO_SCTP == cih->nexthdr)
1480                 offset += 2 * sizeof(__u16);
1481         verdict = ip_vs_icmp_xmit_v6(skb, cp, pp, offset, hooknum);
1482
1483         __ip_vs_conn_put(cp);
1484
1485         return verdict;
1486 }
1487 #endif
1488
1489
1490 /*
1491  *      Check if it's for virtual services, look it up,
1492  *      and send it on its way...
1493  */
1494 static unsigned int
1495 ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1496 {
1497         struct net *net;
1498         struct ip_vs_iphdr iph;
1499         struct ip_vs_protocol *pp;
1500         struct ip_vs_proto_data *pd;
1501         struct ip_vs_conn *cp;
1502         int ret, pkts;
1503         struct netns_ipvs *ipvs;
1504
1505         /* Already marked as IPVS request or reply? */
1506         if (skb->ipvs_property)
1507                 return NF_ACCEPT;
1508
1509         /*
1510          *      Big tappo:
1511          *      - remote client: only PACKET_HOST
1512          *      - route: used for struct net when skb->dev is unset
1513          */
1514         if (unlikely((skb->pkt_type != PACKET_HOST &&
1515                       hooknum != NF_INET_LOCAL_OUT) ||
1516                      !skb_dst(skb))) {
1517                 ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1518                 IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s"
1519                               " ignored in hook %u\n",
1520                               skb->pkt_type, iph.protocol,
1521                               IP_VS_DBG_ADDR(af, &iph.daddr), hooknum);
1522                 return NF_ACCEPT;
1523         }
1524         /* ipvs enabled in this netns ? */
1525         net = skb_net(skb);
1526         if (!net_ipvs(net)->enable)
1527                 return NF_ACCEPT;
1528
1529         ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1530
1531         /* Bad... Do not break raw sockets */
1532         if (unlikely(skb->sk != NULL && hooknum == NF_INET_LOCAL_OUT &&
1533                      af == AF_INET)) {
1534                 struct sock *sk = skb->sk;
1535                 struct inet_sock *inet = inet_sk(skb->sk);
1536
1537                 if (inet && sk->sk_family == PF_INET && inet->nodefrag)
1538                         return NF_ACCEPT;
1539         }
1540
1541 #ifdef CONFIG_IP_VS_IPV6
1542         if (af == AF_INET6) {
1543                 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1544                         int related;
1545                         int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum);
1546
1547                         if (related)
1548                                 return verdict;
1549                         ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1550                 }
1551         } else
1552 #endif
1553                 if (unlikely(iph.protocol == IPPROTO_ICMP)) {
1554                         int related;
1555                         int verdict = ip_vs_in_icmp(skb, &related, hooknum);
1556
1557                         if (related)
1558                                 return verdict;
1559                         ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
1560                 }
1561
1562         /* Protocol supported? */
1563         pd = ip_vs_proto_data_get(net, iph.protocol);
1564         if (unlikely(!pd))
1565                 return NF_ACCEPT;
1566         pp = pd->pp;
1567         /*
1568          * Check if the packet belongs to an existing connection entry
1569          */
1570         cp = pp->conn_in_get(af, skb, &iph, iph.len, 0);
1571
1572         if (unlikely(!cp)) {
1573                 int v;
1574
1575                 if (!pp->conn_schedule(af, skb, pd, &v, &cp))
1576                         return v;
1577         }
1578
1579         if (unlikely(!cp)) {
1580                 /* sorry, all this trouble for a no-hit :) */
1581                 IP_VS_DBG_PKT(12, af, pp, skb, 0,
1582                               "ip_vs_in: packet continues traversal as normal");
1583                 return NF_ACCEPT;
1584         }
1585
1586         IP_VS_DBG_PKT(11, af, pp, skb, 0, "Incoming packet");
1587         ipvs = net_ipvs(net);
1588         /* Check the server status */
1589         if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
1590                 /* the destination server is not available */
1591
1592                 if (sysctl_expire_nodest_conn(ipvs)) {
1593                         /* try to expire the connection immediately */
1594                         ip_vs_conn_expire_now(cp);
1595                 }
1596                 /* don't restart its timer, and silently
1597                    drop the packet. */
1598                 __ip_vs_conn_put(cp);
1599                 return NF_DROP;
1600         }
1601
1602         ip_vs_in_stats(cp, skb);
1603         ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pd);
1604         if (cp->packet_xmit)
1605                 ret = cp->packet_xmit(skb, cp, pp);
1606                 /* do not touch skb anymore */
1607         else {
1608                 IP_VS_DBG_RL("warning: packet_xmit is null");
1609                 ret = NF_ACCEPT;
1610         }
1611
1612         /* Increase its packet counter and check if it is needed
1613          * to be synchronized
1614          *
1615          * Sync connection if it is about to close to
1616          * encorage the standby servers to update the connections timeout
1617          *
1618          * For ONE_PKT let ip_vs_sync_conn() do the filter work.
1619          */
1620
1621         if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
1622                 pkts = sysctl_sync_threshold(ipvs);
1623         else
1624                 pkts = atomic_add_return(1, &cp->in_pkts);
1625
1626         if ((ipvs->sync_state & IP_VS_STATE_MASTER) &&
1627             cp->protocol == IPPROTO_SCTP) {
1628                 if ((cp->state == IP_VS_SCTP_S_ESTABLISHED &&
1629                         (pkts % sysctl_sync_period(ipvs)
1630                          == sysctl_sync_threshold(ipvs))) ||
1631                                 (cp->old_state != cp->state &&
1632                                  ((cp->state == IP_VS_SCTP_S_CLOSED) ||
1633                                   (cp->state == IP_VS_SCTP_S_SHUT_ACK_CLI) ||
1634                                   (cp->state == IP_VS_SCTP_S_SHUT_ACK_SER)))) {
1635                         ip_vs_sync_conn(net, cp);
1636                         goto out;
1637                 }
1638         }
1639
1640         /* Keep this block last: TCP and others with pp->num_states <= 1 */
1641         else if ((ipvs->sync_state & IP_VS_STATE_MASTER) &&
1642             (((cp->protocol != IPPROTO_TCP ||
1643                cp->state == IP_VS_TCP_S_ESTABLISHED) &&
1644               (pkts % sysctl_sync_period(ipvs)
1645                == sysctl_sync_threshold(ipvs))) ||
1646              ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
1647               ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
1648                (cp->state == IP_VS_TCP_S_CLOSE) ||
1649                (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
1650                (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
1651                 ip_vs_sync_conn(net, cp);
1652 out:
1653         cp->old_state = cp->state;
1654
1655         ip_vs_conn_put(cp);
1656         return ret;
1657 }
1658
1659 /*
1660  *      AF_INET handler in NF_INET_LOCAL_IN chain
1661  *      Schedule and forward packets from remote clients
1662  */
1663 static unsigned int
1664 ip_vs_remote_request4(unsigned int hooknum, struct sk_buff *skb,
1665                       const struct net_device *in,
1666                       const struct net_device *out,
1667                       int (*okfn)(struct sk_buff *))
1668 {
1669         return ip_vs_in(hooknum, skb, AF_INET);
1670 }
1671
1672 /*
1673  *      AF_INET handler in NF_INET_LOCAL_OUT chain
1674  *      Schedule and forward packets from local clients
1675  */
1676 static unsigned int
1677 ip_vs_local_request4(unsigned int hooknum, struct sk_buff *skb,
1678                      const struct net_device *in, const struct net_device *out,
1679                      int (*okfn)(struct sk_buff *))
1680 {
1681         unsigned int verdict;
1682
1683         /* Disable BH in LOCAL_OUT until all places are fixed */
1684         local_bh_disable();
1685         verdict = ip_vs_in(hooknum, skb, AF_INET);
1686         local_bh_enable();
1687         return verdict;
1688 }
1689
1690 #ifdef CONFIG_IP_VS_IPV6
1691
1692 /*
1693  *      AF_INET6 handler in NF_INET_LOCAL_IN chain
1694  *      Schedule and forward packets from remote clients
1695  */
1696 static unsigned int
1697 ip_vs_remote_request6(unsigned int hooknum, struct sk_buff *skb,
1698                       const struct net_device *in,
1699                       const struct net_device *out,
1700                       int (*okfn)(struct sk_buff *))
1701 {
1702         return ip_vs_in(hooknum, skb, AF_INET6);
1703 }
1704
1705 /*
1706  *      AF_INET6 handler in NF_INET_LOCAL_OUT chain
1707  *      Schedule and forward packets from local clients
1708  */
1709 static unsigned int
1710 ip_vs_local_request6(unsigned int hooknum, struct sk_buff *skb,
1711                      const struct net_device *in, const struct net_device *out,
1712                      int (*okfn)(struct sk_buff *))
1713 {
1714         unsigned int verdict;
1715
1716         /* Disable BH in LOCAL_OUT until all places are fixed */
1717         local_bh_disable();
1718         verdict = ip_vs_in(hooknum, skb, AF_INET6);
1719         local_bh_enable();
1720         return verdict;
1721 }
1722
1723 #endif
1724
1725
1726 /*
1727  *      It is hooked at the NF_INET_FORWARD chain, in order to catch ICMP
1728  *      related packets destined for 0.0.0.0/0.
1729  *      When fwmark-based virtual service is used, such as transparent
1730  *      cache cluster, TCP packets can be marked and routed to ip_vs_in,
1731  *      but ICMP destined for 0.0.0.0/0 cannot not be easily marked and
1732  *      sent to ip_vs_in_icmp. So, catch them at the NF_INET_FORWARD chain
1733  *      and send them to ip_vs_in_icmp.
1734  */
1735 static unsigned int
1736 ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
1737                    const struct net_device *in, const struct net_device *out,
1738                    int (*okfn)(struct sk_buff *))
1739 {
1740         int r;
1741         struct net *net;
1742
1743         if (ip_hdr(skb)->protocol != IPPROTO_ICMP)
1744                 return NF_ACCEPT;
1745
1746         /* ipvs enabled in this netns ? */
1747         net = skb_net(skb);
1748         if (!net_ipvs(net)->enable)
1749                 return NF_ACCEPT;
1750
1751         return ip_vs_in_icmp(skb, &r, hooknum);
1752 }
1753
1754 #ifdef CONFIG_IP_VS_IPV6
1755 static unsigned int
1756 ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1757                       const struct net_device *in, const struct net_device *out,
1758                       int (*okfn)(struct sk_buff *))
1759 {
1760         int r;
1761         struct net *net;
1762
1763         if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6)
1764                 return NF_ACCEPT;
1765
1766         /* ipvs enabled in this netns ? */
1767         net = skb_net(skb);
1768         if (!net_ipvs(net)->enable)
1769                 return NF_ACCEPT;
1770
1771         return ip_vs_in_icmp_v6(skb, &r, hooknum);
1772 }
1773 #endif
1774
1775
1776 static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
1777         /* After packet filtering, change source only for VS/NAT */
1778         {
1779                 .hook           = ip_vs_reply4,
1780                 .owner          = THIS_MODULE,
1781                 .pf             = PF_INET,
1782                 .hooknum        = NF_INET_LOCAL_IN,
1783                 .priority       = NF_IP_PRI_NAT_SRC - 2,
1784         },
1785         /* After packet filtering, forward packet through VS/DR, VS/TUN,
1786          * or VS/NAT(change destination), so that filtering rules can be
1787          * applied to IPVS. */
1788         {
1789                 .hook           = ip_vs_remote_request4,
1790                 .owner          = THIS_MODULE,
1791                 .pf             = PF_INET,
1792                 .hooknum        = NF_INET_LOCAL_IN,
1793                 .priority       = NF_IP_PRI_NAT_SRC - 1,
1794         },
1795         /* Before ip_vs_in, change source only for VS/NAT */
1796         {
1797                 .hook           = ip_vs_local_reply4,
1798                 .owner          = THIS_MODULE,
1799                 .pf             = PF_INET,
1800                 .hooknum        = NF_INET_LOCAL_OUT,
1801                 .priority       = NF_IP_PRI_NAT_DST + 1,
1802         },
1803         /* After mangle, schedule and forward local requests */
1804         {
1805                 .hook           = ip_vs_local_request4,
1806                 .owner          = THIS_MODULE,
1807                 .pf             = PF_INET,
1808                 .hooknum        = NF_INET_LOCAL_OUT,
1809                 .priority       = NF_IP_PRI_NAT_DST + 2,
1810         },
1811         /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1812          * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1813         {
1814                 .hook           = ip_vs_forward_icmp,
1815                 .owner          = THIS_MODULE,
1816                 .pf             = PF_INET,
1817                 .hooknum        = NF_INET_FORWARD,
1818                 .priority       = 99,
1819         },
1820         /* After packet filtering, change source only for VS/NAT */
1821         {
1822                 .hook           = ip_vs_reply4,
1823                 .owner          = THIS_MODULE,
1824                 .pf             = PF_INET,
1825                 .hooknum        = NF_INET_FORWARD,
1826                 .priority       = 100,
1827         },
1828 #ifdef CONFIG_IP_VS_IPV6
1829         /* After packet filtering, change source only for VS/NAT */
1830         {
1831                 .hook           = ip_vs_reply6,
1832                 .owner          = THIS_MODULE,
1833                 .pf             = PF_INET6,
1834                 .hooknum        = NF_INET_LOCAL_IN,
1835                 .priority       = NF_IP6_PRI_NAT_SRC - 2,
1836         },
1837         /* After packet filtering, forward packet through VS/DR, VS/TUN,
1838          * or VS/NAT(change destination), so that filtering rules can be
1839          * applied to IPVS. */
1840         {
1841                 .hook           = ip_vs_remote_request6,
1842                 .owner          = THIS_MODULE,
1843                 .pf             = PF_INET6,
1844                 .hooknum        = NF_INET_LOCAL_IN,
1845                 .priority       = NF_IP6_PRI_NAT_SRC - 1,
1846         },
1847         /* Before ip_vs_in, change source only for VS/NAT */
1848         {
1849                 .hook           = ip_vs_local_reply6,
1850                 .owner          = THIS_MODULE,
1851                 .pf             = PF_INET,
1852                 .hooknum        = NF_INET_LOCAL_OUT,
1853                 .priority       = NF_IP6_PRI_NAT_DST + 1,
1854         },
1855         /* After mangle, schedule and forward local requests */
1856         {
1857                 .hook           = ip_vs_local_request6,
1858                 .owner          = THIS_MODULE,
1859                 .pf             = PF_INET6,
1860                 .hooknum        = NF_INET_LOCAL_OUT,
1861                 .priority       = NF_IP6_PRI_NAT_DST + 2,
1862         },
1863         /* After packet filtering (but before ip_vs_out_icmp), catch icmp
1864          * destined for 0.0.0.0/0, which is for incoming IPVS connections */
1865         {
1866                 .hook           = ip_vs_forward_icmp_v6,
1867                 .owner          = THIS_MODULE,
1868                 .pf             = PF_INET6,
1869                 .hooknum        = NF_INET_FORWARD,
1870                 .priority       = 99,
1871         },
1872         /* After packet filtering, change source only for VS/NAT */
1873         {
1874                 .hook           = ip_vs_reply6,
1875                 .owner          = THIS_MODULE,
1876                 .pf             = PF_INET6,
1877                 .hooknum        = NF_INET_FORWARD,
1878                 .priority       = 100,
1879         },
1880 #endif
1881 };
1882 /*
1883  *      Initialize IP Virtual Server netns mem.
1884  */
1885 static int __net_init __ip_vs_init(struct net *net)
1886 {
1887         struct netns_ipvs *ipvs;
1888
1889         ipvs = net_generic(net, ip_vs_net_id);
1890         if (ipvs == NULL)
1891                 return -ENOMEM;
1892
1893         /* Hold the beast until a service is registerd */
1894         ipvs->enable = 0;
1895         ipvs->net = net;
1896         /* Counters used for creating unique names */
1897         ipvs->gen = atomic_read(&ipvs_netns_cnt);
1898         atomic_inc(&ipvs_netns_cnt);
1899         net->ipvs = ipvs;
1900
1901         if (ip_vs_estimator_net_init(net) < 0)
1902                 goto estimator_fail;
1903
1904         if (ip_vs_control_net_init(net) < 0)
1905                 goto control_fail;
1906
1907         if (ip_vs_protocol_net_init(net) < 0)
1908                 goto protocol_fail;
1909
1910         if (ip_vs_app_net_init(net) < 0)
1911                 goto app_fail;
1912
1913         if (ip_vs_conn_net_init(net) < 0)
1914                 goto conn_fail;
1915
1916         if (ip_vs_sync_net_init(net) < 0)
1917                 goto sync_fail;
1918
1919         printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
1920                          sizeof(struct netns_ipvs), ipvs->gen);
1921         return 0;
1922 /*
1923  * Error handling
1924  */
1925
1926 sync_fail:
1927         ip_vs_conn_net_cleanup(net);
1928 conn_fail:
1929         ip_vs_app_net_cleanup(net);
1930 app_fail:
1931         ip_vs_protocol_net_cleanup(net);
1932 protocol_fail:
1933         ip_vs_control_net_cleanup(net);
1934 control_fail:
1935         ip_vs_estimator_net_cleanup(net);
1936 estimator_fail:
1937         return -ENOMEM;
1938 }
1939
1940 static void __net_exit __ip_vs_cleanup(struct net *net)
1941 {
1942         ip_vs_service_net_cleanup(net); /* ip_vs_flush() with locks */
1943         ip_vs_conn_net_cleanup(net);
1944         ip_vs_app_net_cleanup(net);
1945         ip_vs_protocol_net_cleanup(net);
1946         ip_vs_control_net_cleanup(net);
1947         ip_vs_estimator_net_cleanup(net);
1948         IP_VS_DBG(2, "ipvs netns %d released\n", net_ipvs(net)->gen);
1949 }
1950
1951 static void __net_exit __ip_vs_dev_cleanup(struct net *net)
1952 {
1953         EnterFunction(2);
1954         net_ipvs(net)->enable = 0;      /* Disable packet reception */
1955         smp_wmb();
1956         ip_vs_sync_net_cleanup(net);
1957         LeaveFunction(2);
1958 }
1959
1960 static struct pernet_operations ipvs_core_ops = {
1961         .init = __ip_vs_init,
1962         .exit = __ip_vs_cleanup,
1963         .id   = &ip_vs_net_id,
1964         .size = sizeof(struct netns_ipvs),
1965 };
1966
1967 static struct pernet_operations ipvs_core_dev_ops = {
1968         .exit = __ip_vs_dev_cleanup,
1969 };
1970
1971 /*
1972  *      Initialize IP Virtual Server
1973  */
1974 static int __init ip_vs_init(void)
1975 {
1976         int ret;
1977
1978         ret = ip_vs_control_init();
1979         if (ret < 0) {
1980                 pr_err("can't setup control.\n");
1981                 goto exit;
1982         }
1983
1984         ip_vs_protocol_init();
1985
1986         ret = ip_vs_conn_init();
1987         if (ret < 0) {
1988                 pr_err("can't setup connection table.\n");
1989                 goto cleanup_protocol;
1990         }
1991
1992         ret = register_pernet_subsys(&ipvs_core_ops);   /* Alloc ip_vs struct */
1993         if (ret < 0)
1994                 goto cleanup_conn;
1995
1996         ret = register_pernet_device(&ipvs_core_dev_ops);
1997         if (ret < 0)
1998                 goto cleanup_sub;
1999
2000         ret = nf_register_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2001         if (ret < 0) {
2002                 pr_err("can't register hooks.\n");
2003                 goto cleanup_dev;
2004         }
2005
2006         ret = ip_vs_register_nl_ioctl();
2007         if (ret < 0) {
2008                 pr_err("can't register netlink/ioctl.\n");
2009                 goto cleanup_hooks;
2010         }
2011
2012         pr_info("ipvs loaded.\n");
2013
2014         return ret;
2015
2016 cleanup_hooks:
2017         nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2018 cleanup_dev:
2019         unregister_pernet_device(&ipvs_core_dev_ops);
2020 cleanup_sub:
2021         unregister_pernet_subsys(&ipvs_core_ops);
2022 cleanup_conn:
2023         ip_vs_conn_cleanup();
2024 cleanup_protocol:
2025         ip_vs_protocol_cleanup();
2026         ip_vs_control_cleanup();
2027 exit:
2028         return ret;
2029 }
2030
2031 static void __exit ip_vs_cleanup(void)
2032 {
2033         ip_vs_unregister_nl_ioctl();
2034         nf_unregister_hooks(ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
2035         unregister_pernet_device(&ipvs_core_dev_ops);
2036         unregister_pernet_subsys(&ipvs_core_ops);       /* free ip_vs struct */
2037         ip_vs_conn_cleanup();
2038         ip_vs_protocol_cleanup();
2039         ip_vs_control_cleanup();
2040         pr_info("ipvs unloaded.\n");
2041 }
2042
2043 module_init(ip_vs_init);
2044 module_exit(ip_vs_cleanup);
2045 MODULE_LICENSE("GPL");