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